Author: russellm
Date: 2009-04-11 07:09:34 -0500 (Sat, 11 Apr 2009)
New Revision: 10521

Modified:
   django/trunk/django/db/models/sql/query.py
   django/trunk/tests/regressiontests/aggregation_regress/models.py
Log:
Fixed #10766 -- Raise an error when annotate() references another aggreagte(). 
Thanks to aseer...@mit.edu for the report.

Modified: django/trunk/django/db/models/sql/query.py
===================================================================
--- django/trunk/django/db/models/sql/query.py  2009-04-11 12:07:52 UTC (rev 
10520)
+++ django/trunk/django/db/models/sql/query.py  2009-04-11 12:09:34 UTC (rev 
10521)
@@ -1403,6 +1403,9 @@
             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

Modified: django/trunk/tests/regressiontests/aggregation_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/aggregation_regress/models.py    
2009-04-11 12:07:52 UTC (rev 10520)
+++ django/trunk/tests/regressiontests/aggregation_regress/models.py    
2009-04-11 12:09:34 UTC (rev 10521)
@@ -297,6 +297,11 @@
 >>> 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
 
 """
 }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to