Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 0fff47c90f01c33ffdd0e8ed8c4a623693b7d4dd 1 parent b1a5db3
Russell Keith-Magee authored April 11, 2009
3  django/db/models/sql/query.py
@@ -1403,6 +1403,9 @@ def add_aggregate(self, aggregate, model, alias, is_summary):
1403 1403
             field_name = field_list[0]
1404 1404
             col = field_name
1405 1405
             source = self.aggregates[field_name]
  1406
+            if not is_summary:
  1407
+                raise FieldError("Cannot compute %s('%s'): '%s' is an aggregate" % (
  1408
+                    aggregate.name, field_name, field_name))
1406 1409
         elif ((len(field_list) > 1) or
1407 1410
             (field_list[0] not in [i.name for i in opts.fields]) or
1408 1411
             self.group_by is None or
5  tests/regressiontests/aggregation_regress/models.py
@@ -297,6 +297,11 @@ def __unicode__(self):
297 297
 >>> HardbackBook.objects.annotate(n_authors=Count('authors')).values('name','n_authors')
298 298
 [{'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'}]
299 299
 
  300
+# Regression for #10766 - Shouldn't be able to reference an aggregate fields in an an aggregate() call.
  301
+>>> Book.objects.all().annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
  302
+Traceback (most recent call last):
  303
+...
  304
+FieldError: Cannot compute Avg('mean_age'): 'mean_age' is an aggregate
300 305
 
301 306
 """
302 307
 }

0 notes on commit 0fff47c

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