Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7096 -- The simplifications in [7461] weren't complete. They b…

…roke

multi-component exclude() calls. Fixed that.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7493 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e07a457c0054fa20dcbbf5bf41bc07191bdaa895 1 parent a1e4b15
Malcolm Tredinnick authored April 28, 2008
17  django/db/models/sql/query.py
@@ -895,9 +895,15 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
895 895
         Add a single filter to the query. The 'filter_expr' is a pair:
896 896
         (filter_string, value). E.g. ('name__contains', 'fred')
897 897
 
898  
-        If 'negate' is True, this is an exclude() filter. If 'trim' is True, we
899  
-        automatically trim the final join group (used internally when
900  
-        constructing nested queries).
  898
+        If 'negate' is True, this is an exclude() filter. It's important to
  899
+        note that this method does not negate anything in the where-clause
  900
+        object when inserting the filter constraints. This is because negated
  901
+        filters often require multiple calls to add_filter() and the negation
  902
+        should only happen once. So the caller is responsible for this (the
  903
+        caller will normally be add_q(), so that as an example).
  904
+
  905
+        If 'trim' is True, we automatically trim the final join group (used
  906
+        internally when constructing nested queries).
901 907
 
902 908
         If 'can_reuse' is a set, we are processing a component of a
903 909
         multi-component filter (e.g. filter(Q1, Q2)). In this case, 'can_reuse'
@@ -1001,7 +1007,6 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
1001 1007
 
1002 1008
         self.where.add((alias, col, field, lookup_type, value), connector)
1003 1009
         if negate:
1004  
-            self.where.negate()
1005 1010
             for alias in join_list:
1006 1011
                 self.promote_alias(alias)
1007 1012
             if final > 1 and lookup_type != 'isnull':
@@ -1039,12 +1044,12 @@ def add_q(self, q_object, used_aliases=None):
1039 1044
                 self.where.start_subtree(connector)
1040 1045
                 self.add_q(child, used_aliases)
1041 1046
                 self.where.end_subtree()
1042  
-                if q_object.negated:
1043  
-                    self.where.children[-1].negate()
1044 1047
             else:
1045 1048
                 self.add_filter(child, connector, q_object.negated,
1046 1049
                         can_reuse=used_aliases)
1047 1050
             connector = q_object.connector
  1051
+        if q_object.negated:
  1052
+            self.where.negate()
1048 1053
         if subtree:
1049 1054
             self.where.end_subtree()
1050 1055
 
19  tests/regressiontests/queries/models.py
@@ -658,5 +658,24 @@ class Meta:
658 658
 works.
659 659
 >>> Item.objects.values('note__note').order_by('queries_note.note', 'id')
660 660
 [{'note__note': u'n2'}, {'note__note': u'n3'}, {'note__note': u'n3'}, {'note__note': u'n3'}]
  661
+
  662
+Bug #7096 -- Make sure exclude() with multiple conditions continues to work.
  663
+>>> Tag.objects.filter(parent=t1, name='t3').order_by('name')
  664
+[<Tag: t3>]
  665
+>>> Tag.objects.exclude(parent=t1, name='t3').order_by('name')
  666
+[<Tag: t1>, <Tag: t2>, <Tag: t4>, <Tag: t5>]
  667
+>>> Item.objects.exclude(tags__name='t1', name='one').order_by('name').distinct()
  668
+[<Item: four>, <Item: three>, <Item: two>]
  669
+>>> Item.objects.filter(name__in=['three', 'four']).exclude(tags__name='t1').order_by('name')
  670
+[<Item: four>, <Item: three>]
  671
+
  672
+More twisted cases, involving nested negations.
  673
+>>> Item.objects.exclude(~Q(tags__name='t1', name='one'))
  674
+[<Item: one>]
  675
+>>> Item.objects.filter(~Q(tags__name='t1', name='one'), name='two')
  676
+[<Item: two>]
  677
+>>> Item.objects.exclude(~Q(tags__name='t1', name='one'), name='two')
  678
+[<Item: four>, <Item: one>, <Item: three>]
  679
+
661 680
 """}
662 681
 

0 notes on commit e07a457

Please sign in to comment.
Something went wrong with that request. Please try again.