Browse files

Fixed #10766 -- Raise an error when annotate() references another agg…

…reagte(). Thanks to aseering@mit.edu for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10521 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent b1a5db3 commit 0fff47c90f01c33ffdd0e8ed8c4a623693b7d4dd @freakboy3742 freakboy3742 committed Apr 11, 2009
Showing with 8 additions and 0 deletions.
  1. +3 −0 django/db/models/sql/query.py
  2. +5 −0 tests/regressiontests/aggregation_regress/models.py
View
3 django/db/models/sql/query.py
@@ -1403,6 +1403,9 @@ def add_aggregate(self, aggregate, model, alias, is_summary):
field_name = field_list[0]
col = field_name
source = self.aggregates[field_name]
+ if not is_summary:
+ raise FieldError("Cannot compute %s('%s'): '%s' is an aggregate" % (
+ aggregate.name, field_name, field_name))
elif ((len(field_list) > 1) or
(field_list[0] not in [i.name for i in opts.fields]) or
self.group_by is None or
View
5 tests/regressiontests/aggregation_regress/models.py
@@ -297,6 +297,11 @@ def __unicode__(self):
>>> HardbackBook.objects.annotate(n_authors=Count('authors')).values('name','n_authors')
[{'n_authors': 2, 'name': u'Artificial Intelligence: A Modern Approach'}, {'n_authors': 1, 'name': u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'}]
+# Regression for #10766 - Shouldn't be able to reference an aggregate fields in an an aggregate() call.
+>>> Book.objects.all().annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
+Traceback (most recent call last):
+...
+FieldError: Cannot compute Avg('mean_age'): 'mean_age' is an aggregate
"""
}

0 comments on commit 0fff47c

Please sign in to comment.