Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10100 -- Corrected handling of the negation required exclude()…

… clauses that reference aggregate columns. Thanks to Anossov for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9785 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit ed3d2735a2eb5d279e2098e14437eb9e445498c6 1 parent ef22bb7
Russell Keith-Magee authored January 23, 2009
6  django/db/models/sql/query.py
@@ -1266,7 +1266,11 @@ def add_filter(self, filter_expr, connector=AND, negate=False, trim=False,
1266 1266
 
1267 1267
         for alias, aggregate in self.aggregate_select.items():
1268 1268
             if alias == parts[0]:
1269  
-                self.having.add((aggregate, lookup_type, value), AND)
  1269
+                entry = self.where_class()
  1270
+                entry.add((aggregate, lookup_type, value), AND)
  1271
+                if negate:
  1272
+                    entry.negate()
  1273
+                self.having.add(entry, AND)
1270 1274
                 return
1271 1275
 
1272 1276
         opts = self.get_meta()
13  tests/regressiontests/aggregation_regress/models.py
@@ -151,6 +151,19 @@ def __unicode__(self):
151 151
 >>> Book.objects.select_related('publisher').annotate(num_authors=Count('authors')).values()[0]
152 152
 {'rating': 4.0, 'isbn': u'013790395', 'name': u'Artificial Intelligence: A Modern Approach', 'pubdate': datetime.date(1995, 1, 15), 'price': Decimal("82.8..."), 'id': 5, 'num_authors': 2, 'publisher_id': 3, 'pages': 1132}
153 153
 
  154
+# Regression for #10010: exclude on an aggregate field is correctly negated
  155
+>>> len(Book.objects.annotate(num_authors=Count('authors')))
  156
+6
  157
+>>> len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__gt=2))
  158
+1
  159
+>>> len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__gt=2))
  160
+5
  161
+
  162
+>>> len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__lt=3).exclude(num_authors__lt=2))
  163
+2
  164
+>>> len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__lt=2).filter(num_authors__lt=3))
  165
+2
  166
+
154 167
 """
155 168
 }
156 169
 

0 notes on commit ed3d273

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