Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #7076 -- Include NULL values when excluding non-NULL items.

Based on a patch from emulbreh.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7760 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e41df5adccef0e32d4b8b877ec83e9dd271c1ccf 1 parent 2c08986
@malcolmt malcolmt authored
View
26 django/db/models/sql/query.py
@@ -1045,17 +1045,27 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
self.promote_alias(table)
self.where.add((alias, col, field, lookup_type, value), connector)
+
if negate:
for alias in join_list:
self.promote_alias(alias)
- if final > 1 and lookup_type != 'isnull':
- for alias in join_list:
- if self.alias_map[alias] == self.LOUTER:
- j_col = self.alias_map[alias][RHS_JOIN_COL]
- entry = Node([(alias, j_col, None, 'isnull', True)])
- entry.negate()
- self.where.add(entry, AND)
- break
+ if lookup_type != 'isnull':
+ if final > 1:
+ for alias in join_list:
+ if self.alias_map[alias][JOIN_TYPE] == self.LOUTER:
+ j_col = self.alias_map[alias][RHS_JOIN_COL]
+ entry = Node([(alias, j_col, None, 'isnull', True)])
+ entry.negate()
+ self.where.add(entry, AND)
+ break
+ elif not (lookup_type == 'in' and not value):
+ # Leaky abstraction artifact: We have to specifically
+ # exclude the "foo__in=[]" case from this handling, because
+ # it's short-circuited in the Where class.
+ entry = Node([(alias, col, field, 'isnull', True)])
+ entry.negate()
+ self.where.add(entry, AND)
+
if can_reuse is not None:
can_reuse.update(join_list)
View
6 tests/regressiontests/queries/models.py
@@ -756,5 +756,11 @@ class Child(models.Model):
>>> obj.person.details.data
u'd2'
+Bug #7076 -- excluding shouldn't eliminate NULL entries.
+>>> Item.objects.exclude(modified=time1).order_by('name')
+[<Item: four>, <Item: three>, <Item: two>]
+>>> Tag.objects.exclude(parent__name=t1.name)
+[<Tag: t1>, <Tag: t4>, <Tag: t5>]
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.