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.