summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-01-23 07:07:51 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-01-23 07:07:51 +0000
commited3d2735a2eb5d279e2098e14437eb9e445498c6 (patch)
tree89c98d075628f643770630c2e18ea2bcf001701d
parentef22bb7b3397932758bad0dc706723693ce85cb3 (diff)
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
-rw-r--r--django/db/models/sql/query.py6
-rw-r--r--tests/regressiontests/aggregation_regress/models.py13
2 files changed, 18 insertions, 1 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index b961ac9eeb..8a4f88a9c8 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1266,7 +1266,11 @@ class BaseQuery(object):
for alias, aggregate in self.aggregate_select.items():
if alias == parts[0]:
- self.having.add((aggregate, lookup_type, value), AND)
+ entry = self.where_class()
+ entry.add((aggregate, lookup_type, value), AND)
+ if negate:
+ entry.negate()
+ self.having.add(entry, AND)
return
opts = self.get_meta()
diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
index ca2447f505..8a590dca8b 100644
--- a/tests/regressiontests/aggregation_regress/models.py
+++ b/tests/regressiontests/aggregation_regress/models.py
@@ -151,6 +151,19 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, id, i
>>> Book.objects.select_related('publisher').annotate(num_authors=Count('authors')).values()[0]
{'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}
+# Regression for #10010: exclude on an aggregate field is correctly negated
+>>> len(Book.objects.annotate(num_authors=Count('authors')))
+6
+>>> len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__gt=2))
+1
+>>> len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__gt=2))
+5
+
+>>> len(Book.objects.annotate(num_authors=Count('authors')).filter(num_authors__lt=3).exclude(num_authors__lt=2))
+2
+>>> len(Book.objects.annotate(num_authors=Count('authors')).exclude(num_authors__lt=2).filter(num_authors__lt=3))
+2
+
"""
}