Hi Russ, Thanks for a quick response.
I don't think Django needs to make the decision of whether or not to do a join. That can be done by the underlying database as long as Django generates a query that the database can understand and optimize properly. In this case, it would imply that Django would still need to generate a join, but check the null condition not on the primary key field but on one of the fields the join condition. Regardless, I'm going to file a ticket on this. Thanks again. D. On Nov 19, 2:36 am, Russell Keith-Magee <russ...@keith-magee.com> wrote: > On Sat, Nov 19, 2011 at 8:41 AM, dmitry b <dmitry.ma...@gmail.com> wrote: > > Is there a recommended approach to anti-join queries? Here's the > > query I'm having an issue with: > > > Branch.objects.filter(branchgroup__isnull=True)[:1] > > > where branchgroup is a ManytoMany relationship to another model. I > > want to get a set of Branch objects that aren't mapped to any > > BranchGroups. > > Hi Dmitry, > > There isn't any specific way to force the non-creation of a join > condition; Django doesn't expose the internals of query construction, > and should be choosing the optimal SQL for the ORM query. > > The sort of optimization you describe (i.e., don't do the join if you > can just check the primary key value on the local model) is an > optimization that Django is definitely capable of performing. If the > ORM isn't performing this optimization in this case, it's possible > you've found a bug. > > There are some cases where the optimization *isn't* possible, but > without seeing the models for your test case, it's impossible to say > for certain. > > If you think you've found a way that Django could optimize it's > queries better, the best way forward is to open a ticket, with a set > of sample models and the expected/actual query that is generated for > those specific models. > > Yours, > Russ Magee %-) -- 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.