Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14476 -- Fixed resolution of automatically generated annotatio…

…n names so e.g. filtering based on them works. Thanks dirleyls for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16252 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 15793309e16dcdf5de17594eaef1962a7c35ce31 1 parent 803a454
Ramiro Morales authored
16  django/db/models/sql/query.py
@@ -1053,14 +1053,14 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
1053 1053
             value = SQLEvaluator(value, self)
1054 1054
             having_clause = value.contains_aggregate
1055 1055
 
1056  
-        if parts[0] in self.aggregates:
1057  
-            aggregate = self.aggregates[parts[0]]
1058  
-            entry = self.where_class()
1059  
-            entry.add((aggregate, lookup_type, value), AND)
1060  
-            if negate:
1061  
-                entry.negate()
1062  
-            self.having.add(entry, connector)
1063  
-            return
  1056
+        for alias, aggregate in self.aggregates.items():
  1057
+            if alias in (parts[0], LOOKUP_SEP.join(parts)):
  1058
+                entry = self.where_class()
  1059
+                entry.add((aggregate, lookup_type, value), AND)
  1060
+                if negate:
  1061
+                    entry.negate()
  1062
+                self.having.add(entry, connector)
  1063
+                return
1064 1064
 
1065 1065
         opts = self.get_meta()
1066 1066
         alias = self.get_initial_alias()
27  tests/regressiontests/aggregation_regress/tests.py
@@ -827,3 +827,30 @@ def test_stddev(self):
827 827
             Book.objects.aggregate(Variance('price', sample=True)),
828 828
             {'price__variance': Approximate(700.53, 2)}
829 829
         )
  830
+
  831
+    def test_filtering_by_annotation_name(self):
  832
+        # Regression test for #14476
  833
+
  834
+        # The name of the explicitly provided annotation name in this case
  835
+        # poses no problem
  836
+        qs = Author.objects.annotate(book_cnt=Count('book')).filter(book_cnt=2)
  837
+        self.assertQuerysetEqual(
  838
+            qs,
  839
+            ['Peter Norvig'],
  840
+            lambda b: b.name
  841
+        )
  842
+        # Neither in this case
  843
+        qs = Author.objects.annotate(book_count=Count('book')).filter(book_count=2)
  844
+        self.assertQuerysetEqual(
  845
+            qs,
  846
+            ['Peter Norvig'],
  847
+            lambda b: b.name
  848
+        )
  849
+        # This case used to fail because the ORM couldn't resolve the
  850
+        # automatically generated annotation name `book__count`
  851
+        qs = Author.objects.annotate(Count('book')).filter(book__count=2)
  852
+        self.assertQuerysetEqual(
  853
+            qs,
  854
+            ['Peter Norvig'],
  855
+            lambda b: b.name
  856
+        )

0 notes on commit 1579330

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