On Thu, 2009-09-24 at 06:25 -0700, blumenkraft wrote: > Hi, > > It seems that Django generates too many joins with chained filters: > > class Issue(models.Model): > is_opened = models.BooleanField() > > class Advertisement(models.Model): > issues = models.ManyToManyField(Issue, null = True) > > >>> x = models.Advertisement.objects.filter(issues__is_opened = > >>> True).filter(issues__is_opened = False) > >>> print x._as_sql() > > ('SELECT U0.`id` FROM `publish_advertisement` U0 INNER JOIN > `publish_advertisement_issues` U1 ON (U0.`id` = U1.`advertisement_id`) > INNER JOIN `publish_issue` U2 ON (U1.`issue_id` = U2.`id`) INNER JOIN > `publish_advertisement_issues` U3 ON (U0.`id` = U3.`advertisement_id`) > INNER JOIN `publish_issue` U4 ON (U3.`issue_id` = U4.`id`) WHERE > (U2.`is_opened` = %s AND U4.`is_opened` = %s )', > (True, False)) > > Is it expected behavior? If so how can I bypass it (still using > chained filters)?
How is that too many? An advertisement can have many issues, you are querying for advertisements that have an issue that is_opened and also an issue that is not is_opened. If it did it with only one join to `publish_issue`, it would not have the same meaning. Cheers Tom --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---