Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

queryset-refactor: Fixed a problem when adding certain additional fil…

…ters to a queryset that has precisely one filter attached already.

Refs #6154.


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@6957 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 519178154be44da2ece9ee9609a0217008616454 1 parent 9709194
Malcolm Tredinnick authored December 19, 2007
7  django/db/models/sql/query.py
@@ -792,6 +792,11 @@ def add_q(self, q_object):
792 792
             q_object.add_to_query(self)
793 793
             return
794 794
 
  795
+        if self.where and q_object.connector != AND and len(q_object) > 1:
  796
+            self.where.start_subtree(AND)
  797
+            subtree = True
  798
+        else:
  799
+            subtree = False
795 800
         for child in q_object.children:
796 801
             if isinstance(child, Node):
797 802
                 self.where.start_subtree(q_object.connector)
@@ -799,6 +804,8 @@ def add_q(self, q_object):
799 804
                 self.where.end_subtree()
800 805
             else:
801 806
                 self.add_filter(child, q_object.connector, q_object.negated)
  807
+        if subtree:
  808
+            self.where.end_subtree()
802 809
 
803 810
     def setup_joins(self, names, opts, alias, dupe_multis):
804 811
         """
4  django/utils/tree.py
@@ -87,8 +87,8 @@ def negate(self):
87 87
     def start_subtree(self, conn_type):
88 88
         """
89 89
         Sets up internal state so that new nodes are added to a subtree of the
90  
-        current node. The conn_type is required so that the new subtree is
91  
-        connected correctly to any existing nodes in the tree.
  90
+        current node. The conn_type specifies how the sub-tree is joined to the
  91
+        existing children.
92 92
         """
93 93
         if len(self.children) == 1:
94 94
             self.connector = conn_type
7  tests/regressiontests/queries/models.py
@@ -459,5 +459,12 @@ class Y(models.Model):
459 459
 >>> Item.objects.extra(select={'count': 'select count(*) from queries_item_tags where queries_item_tags.item_id = queries_item.id'}).filter(count=1)
460 460
 [<Item: four>]
461 461
 
  462
+Bug #6154
  463
+Multiple filter statements are joined using "AND" all the time.
  464
+
  465
+>>> Author.objects.filter(id=a1.id).filter(Q(extra__note=n1)|Q(item__note=n3))
  466
+[<Author: a1>]
  467
+>>> Author.objects.filter(Q(extra__note=n1)|Q(item__note=n3)).filter(id=a1.id)
  468
+[<Author: a1>]
462 469
 """}
463 470
 

0 notes on commit 5191781

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