diff --git a/django/db/models/query.py b/django/db/models/query.py index e33a858a814..c06a37897e2 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -307,11 +307,13 @@ class QuerySet(object): for arg in args: kwargs[arg.default_alias] = arg + query = self.query.clone() + for (alias, aggregate_expr) in kwargs.items(): - self.query.add_aggregate(aggregate_expr, self.model, alias, + query.add_aggregate(aggregate_expr, self.model, alias, is_summary=True) - return self.query.get_aggregation() + return query.get_aggregation() def count(self): """ diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py index 24de920290d..fc2c44b09eb 100644 --- a/tests/regressiontests/aggregation_regress/models.py +++ b/tests/regressiontests/aggregation_regress/models.py @@ -200,6 +200,12 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta >>> sorted([(b.name, b.authors__age__avg, b.publisher.name, b.contact.name) for b in books]) [(u'Artificial Intelligence: A Modern Approach', 51.5, u'Prentice Hall', u'Peter Norvig'), (u'Practical Django Projects', 29.0, u'Apress', u'James Bennett'), (u'Python Web Development with Django', 30.3..., u'Prentice Hall', u'Jeffrey Forcier'), (u'Sams Teach Yourself Django in 24 Hours', 45.0, u'Sams', u'Brad Dayley')] +# Regression for #10199 - Aggregate calls clone the original query so the original query can still be used +>>> books = Book.objects.all() +>>> _ = books.aggregate(Avg('authors__age')) +>>> books.all() +[, , , , , ] + """ }