On Mon, 2009-06-01 at 16:01 -0700, Ben Adida wrote: > I have a ManyToMany self relationship with a custom join table (I > don't think the self-reference is important to the problem, but it > might be, so I'm mentioning it): > > class Documents > related_docs = models.ManyToManyField("self", > through='DocumentRels') > > DocumentRels is a table that maps one document to another, with a > relationship type. > > If I want all documents related to doc_0, I can just do: > > doc_0.related_docs > > But what if I want all documents related to doc_0 where I specify an > extra constraint on the join table, e.g. a value for > DocumentRels.relationship? Is there a way to do that without custom > SQL?
You will not need custom sql, because DocumentRels is going to be a Model. When you want to filter based on DocumentRels.relationship, you will either use: DocumentRels.objects.filter() or go through the RelatedManagers that are added to your Document model for the ForeignKeys in DocumentRels. The reference is here: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships For code that exactly fits your case, see PersonSelfRefM2M in: http://code.djangoproject.com/browser/django/trunk/tests/modeltests/m2m_through/models.py sdc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---