Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8046 -- The first filter() call on a related manager for many-…

…to-many

fields no longer creates duplicate copies of the join table(s). Basically, this
means filters on the join table (for ManyToManyField(through=...)) and complex
filters in the normal (non-through) case don't produce incorrect or duplicate
results.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8472 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d4d7bc175ddd0b698e394a202f8ab6ae572d63d3 1 parent 6e36ce1
Malcolm Tredinnick authored August 22, 2008
2  django/db/models/fields/related.py
@@ -376,7 +376,7 @@ def __init__(self, model=None, core_filters=None, instance=None, symmetrical=Non
17  django/db/models/query.py
@@ -121,6 +121,7 @@ def __init__(self, model=None, query=None):
121 121
         self.query = query or sql.Query(self.model, connection)
122 122
         self._result_cache = None
123 123
         self._iter = None
  124
+        self._sticky_filter = False
124 125
 
125 126
     ########################
126 127
     # PYTHON MAGIC METHODS #
@@ -589,7 +590,10 @@ def reverse(self):
589 590
     def _clone(self, klass=None, setup=False, **kwargs):
590 591
         if klass is None:
591 592
             klass = self.__class__
592  
-        c = klass(model=self.model, query=self.query.clone())
  593
+        query = self.query.clone()
  594
+        if self._sticky_filter:
  595
+            query.filter_is_sticky = True
  596
+        c = klass(model=self.model, query=query)
593 597
         c.__dict__.update(kwargs)
594 598
         if setup and hasattr(c, '_setup_query'):
595 599
             c._setup_query()
@@ -607,6 +611,17 @@ def _fill_cache(self, num=None):
607 611
             except StopIteration:
608 612
                 self._iter = None
609 613
 
  614
+    def _next_is_sticky(self):
  615
+        """
  616
+        Indicates that the next filter call and the one following that should
  617
+        be treated as a single filter. This is only important when it comes to
  618
+        determining when to reuse tables for many-to-many filters. Required so
  619
+        that we can filter naturally on the results of related managers.
  620
+        """
  621
+        obj = self._clone()
  622
+        obj._sticky_filter = True
  623
+        return obj
  624
+
610 625
     def _merge_sanity_check(self, other):
611 626
         """
612 627
         Checks that we are merging two comparable QuerySet classes. By default
50  django/db/models/sql/query.py
@@ -58,6 +58,8 @@ def __init__(self, model, connection, where=WhereNode):
58 58
         self.select_fields = []
59 59
         self.related_select_fields = []
60 60
         self.dupe_avoidance = {}
  61
+        self.used_aliases = set()
  62
+        self.filter_is_sticky = False
61 63
 
62 64
         # SQL-related attributes
63 65
         self.select = []
@@ -78,7 +80,7 @@ def __init__(self, model, connection, where=WhereNode):
78 80
 
79 81
         # These are for extensions. The contents are more or less appended
80 82
         # verbatim to the appropriate clause.
81  
-        self.extra_select = SortedDict()  # Maps col_alias -> col_sql.
  83
+        self.extra_select = SortedDict()  # Maps col_alias -> (col_sql, params).
82 84
         self.extra_tables = ()
83 85
         self.extra_where = ()
84 86
         self.extra_params = ()
@@ -185,6 +187,11 @@ def clone(self, klass=None, **kwargs):
185 187
         obj.extra_where = self.extra_where
186 188
         obj.extra_params = self.extra_params
187 189
         obj.extra_order_by = self.extra_order_by
  190
+        if self.filter_is_sticky and self.used_aliases:
  191
+            obj.used_aliases = self.used_aliases.copy()
  192
+        else:
  193
+            obj.used_aliases = set()
  194
+        obj.filter_is_sticky = False
188 195
         obj.__dict__.update(kwargs)
189 196
         if hasattr(obj, '_setup_query'):
190 197
             obj._setup_query()
@@ -1148,31 +1155,32 @@ def add_q(self, q_object, used_aliases=None):
1148 1155
         Can also be used to add anything that has an 'add_to_query()' method.
1149 1156
         """
1150 1157
         if used_aliases is None:
1151  
-            used_aliases = set()
  1158
+            used_aliases = self.used_aliases
1152 1159
         if hasattr(q_object, 'add_to_query'):
1153 1160
             # Complex custom objects are responsible for adding themselves.
1154 1161
             q_object.add_to_query(self, used_aliases)
1155  
-            return
1156  
-
1157  
-        if self.where and q_object.connector != AND and len(q_object) > 1:
1158  
-            self.where.start_subtree(AND)
1159  
-            subtree = True
1160 1162
         else:
1161  
-            subtree = False
1162  
-        connector = AND
1163  
-        for child in q_object.children:
1164  
-            if isinstance(child, Node):
1165  
-                self.where.start_subtree(connector)
1166  
-                self.add_q(child, used_aliases)
1167  
-                self.where.end_subtree()
  1163
+            if self.where and q_object.connector != AND and len(q_object) > 1:
  1164
+                self.where.start_subtree(AND)
  1165
+                subtree = True
1168 1166
             else:
1169  
-                self.add_filter(child, connector, q_object.negated,
1170  
-                        can_reuse=used_aliases)
1171  
-            connector = q_object.connector
1172  
-        if q_object.negated:
1173  
-            self.where.negate()
1174  
-        if subtree:
1175  
-            self.where.end_subtree()
  1167
+                subtree = False
  1168
+            connector = AND
  1169
+            for child in q_object.children:
  1170
+                if isinstance(child, Node):
  1171
+                    self.where.start_subtree(connector)
  1172
+                    self.add_q(child, used_aliases)
  1173
+                    self.where.end_subtree()
  1174
+                else:
  1175
+                    self.add_filter(child, connector, q_object.negated,
  1176
+                            can_reuse=used_aliases)
  1177
+                connector = q_object.connector
  1178
+            if q_object.negated:
  1179
+                self.where.negate()
  1180
+            if subtree:
  1181
+                self.where.end_subtree()
  1182
+        if self.filter_is_sticky:
  1183
+            self.used_aliases = used_aliases
1176 1184
 
1177 1185
     def setup_joins(self, names, opts, alias, dupe_multis, allow_many=True,
1178 1186
             allow_explicit_fk=False, can_reuse=None):
54  tests/regressiontests/m2m_through_regress/models.py
@@ -8,7 +8,7 @@ class Membership(models.Model):
8 8
     person = models.ForeignKey('Person')
9 9
     group = models.ForeignKey('Group')
10 10
     price = models.IntegerField(default=100)
11  
-    
  11
+
12 12
     def __unicode__(self):
13 13
         return "%s is a member of %s" % (self.person.name, self.group.name)
14 14
 
@@ -16,7 +16,7 @@ class UserMembership(models.Model):
16 16
     user = models.ForeignKey(User)
17 17
     group = models.ForeignKey('Group')
18 18
     price = models.IntegerField(default=100)
19  
-    
  19
+
20 20
     def __unicode__(self):
21 21
         return "%s is a user and member of %s" % (self.user.username, self.group.name)
22 22
 
@@ -31,10 +31,10 @@ class Group(models.Model):
31 31
     # Membership object defined as a class
32 32
     members = models.ManyToManyField(Person, through=Membership)
33 33
     user_members = models.ManyToManyField(User, through='UserMembership')
34  
-    
  34
+
35 35
     def __unicode__(self):
36 36
         return self.name
37  
-        
  37
+
38 38
 __test__ = {'API_TESTS':"""
39 39
 # Create some dummy data
40 40
 >>> bob = Person.objects.create(name='Bob')
@@ -46,7 +46,7 @@ def __unicode__(self):
46 46
 >>> frank = User.objects.create_user('frank','frank@example.com','password')
47 47
 >>> jane = User.objects.create_user('jane','jane@example.com','password')
48 48
 
49  
-# Now test that the forward declared Membership works 
  49
+# Now test that the forward declared Membership works
50 50
 >>> Membership.objects.create(person=bob, group=rock)
51 51
 <Membership: Bob is a member of Rock>
52 52
 
@@ -83,7 +83,7 @@ def __unicode__(self):
83 83
 ...
84 84
 AttributeError: Cannot use create() on a ManyToManyField which specifies an intermediary model.  Use Membership's Manager instead.
85 85
 
86  
-# Now test that the intermediate with a relationship outside 
  86
+# Now test that the intermediate with a relationship outside
87 87
 # the current app (i.e., UserMembership) workds
88 88
 >>> UserMembership.objects.create(user=frank, group=rock)
89 89
 <UserMembership: frank is a user and member of Rock>
@@ -100,11 +100,11 @@ def __unicode__(self):
100 100
 >>> roll.user_members.all()
101 101
 [<User: frank>]
102 102
 
103  
-# Regression test for #8134 -- 
  103
+# Regression test for #8134 --
104 104
 # m2m-through models shouldn't be serialized as m2m fields on the model.
105 105
 
106  
-# First, clean up a lot of objects we don't need. 
107  
-# The serialization test only requires three objects to work - 
  106
+# First, clean up a lot of objects we don't need.
  107
+# The serialization test only requires three objects to work -
108 108
 # one for each end of the m2m, plus the through model.
109 109
 
110 110
 >>> User.objects.all().delete()
@@ -117,24 +117,24 @@ def __unicode__(self):
117 117
 >>> management.call_command('dumpdata', 'm2m_through_regress', format='json', indent=2)
118 118
 [
119 119
   {
120  
-    "pk": 2, 
121  
-    "model": "m2m_through_regress.membership", 
  120
+    "pk": 2,
  121
+    "model": "m2m_through_regress.membership",
122 122
     "fields": {
123  
-      "person": 1, 
124  
-      "price": 100, 
  123
+      "person": 1,
  124
+      "price": 100,
125 125
       "group": 2
126 126
     }
127  
-  }, 
  127
+  },
128 128
   {
129  
-    "pk": 1, 
130  
-    "model": "m2m_through_regress.person", 
  129
+    "pk": 1,
  130
+    "model": "m2m_through_regress.person",
131 131
     "fields": {
132 132
       "name": "Bob"
133 133
     }
134  
-  }, 
  134
+  },
135 135
   {
136  
-    "pk": 2, 
137  
-    "model": "m2m_through_regress.group", 
  136
+    "pk": 2,
  137
+    "model": "m2m_through_regress.group",
138 138
     "fields": {
139 139
       "name": "Roll"
140 140
     }
@@ -158,4 +158,18 @@ def __unicode__(self):
158 158
   </object>
159 159
 </django-objects>
160 160
 
161  
-"""}
  161
+## Regression test for #8046:
  162
+Check that we don't involve too many copies of the intermediate table when
  163
+doing a join.
  164
+
  165
+>>> bob = Person.objects.create(name='Bob')
  166
+>>> jim = Person.objects.create(name='Jim')
  167
+>>> rock = Group.objects.create(name='Rock')
  168
+>>> roll = Group.objects.create(name='Roll')
  169
+>>> _ = Membership.objects.create(person=bob, group=rock)
  170
+>>> _ = Membership.objects.create(person=jim, group=rock, price=50)
  171
+>>> _ = Membership.objects.create(person=bob, group=roll, price=50)
  172
+>>> rock.members.filter(membership__price=50)
  173
+[<Person: Jim>]
  174
+
  175
+"""}

0 notes on commit d4d7bc1

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