On Thu, 2007-09-06 at 20:59 +0800, Russell Keith-Magee wrote: > On 9/6/07, omat <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > For Question & Answer models: > > > > Question.objects.filter(answer__isnull=False) > > > > returns the set of questions that has at least one answer, but: > > > > Question.objects.filter(answer__isnull=True) > > > > does not return the questions that has no answer. > > > > Am I missing something? > > Somewhat. The problem is that 'isnull' isn't doing what you think it does. > > When you run Question.objects.filter(answer__isnull=True), this gets > turned into an SQL query something like: > > SELECT Question.* from Question INNER JOIN Answer WHERE Question.id = > Answer.question_id WHERE Answer.id IS NULL
Umm, no it doesn't. :-) It gets turned into a join using LEFT OUTER JOIN always. so entries not appearing in the right hand table will still appear in the result set. Querying isnull=True does work with m2m relation, so perhaps the original poster needs to post the models he is using. One example that shows this works (in this case, Entry is a model with a nullable ManyToManyField called 'tags' that links to the Tag model): In [2]: Entry.objects.filter(tags__isnull=True) Out[2]: [<Entry: Βετα [04 Jul 2007]>, <Entry: The Non-public Default [05 Jun 2006]>, <Entry: Another entry [03 May 2006]>] All those Entry objects do indeed have e.tags.all() being empty. Regards, Malcolm -- Monday is an awful way to spend 1/7th of your life. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---