On Tue, 2009-02-24 at 07:13 -0800, jgen...@jimmy.harvard.edu wrote: > Hi there ... > > Suppose I have three Models: > > Model A > > Model B, has a M2M with Model A > > Model C, has M2Ms with both Model A and Model B > > What I'm trying to do is to construct a query which will answer the > following question: "For a given A, what Cs does it have a > relationship with, either through it's direct M2M or indirectly via > Model B's M2M with Model C". > > I can do this piecemeal by getting the results from the A-C M2M, and > then for each A-B pair get that B's results, and then unioning > everything, but it'd be nice (and presumably The Right Way) to get it > all in one command. It's this step that's confounding me. I'm > guessing it requires using Q, but I still can't quite figure out the > right magic to make this work.
Let's assume the models are described like this: class A(models.Model): pass class B(models.Model): to_a = models.ManyToManyField(A) class C(models.Model): to_a = models.ManyToManyField(A) to_b = mdoels.ManyToManyField(B) and suppose "obj_a" is your given A instance. Then I suspect this is the type of queryset you're after: C.objects.filter(Q(to_a=obj_a) | Q(to_b__to_a=obj_a)) Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---