﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
16112	Excluding some value in a related model excludes objects without that model	Alex Dehnert	nobody	"I have models that look something like:
{{{#!python
class Group(models.Model):
    name = models.CharField(max_length=100)
    activity_category = models.ForeignKey('ActivityCategory', null=True, blank=True, )
class ActivityCategory(models.Model):
    name = models.CharField(max_length=50)
}}}

I'm trying to find each Group that doesn't have some ActivityCategory:
{{{#!python
qobj = Q(activity_category__name='Dorm')
queryset = groups.models.Group.objects.filter(~qobj)
}}}

Intuitively, I would expect this to find both groups with no ActivityCategory and groups where the ActivityCategory exists and it isn't ""Dorm"". Reading https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships seems to support this interpretation --- ""If you are filtering across multiple relationships and one of the intermediate models doesn't have a value that meets the filter condition, Django will treat it as if there is an empty (all values are NULL), but valid, object there."" Unfortunately, the ORM generates a query using INNER JOIN instead of LEFT OUTER JOIN, so my Group with a null ActivityCategory will not be returned.

I can work around this by explicitly indicating that null is possible, but it's annoying and unintuitive:
{{{#!python
qobj = Q(activity_category__isnull = True) | ~(Q(activity_category__name='Dorm') | Q(activity_category__name='FSILG'))
queryset = groups.models.Group.objects.filter(qobj)
}}}"	Bug	closed	Database layer (models, ORM)	1.3	Normal	fixed		Alex Dehnert	Design decision needed	0	0	0	0	0	0
