Skip to content

Commit

Permalink
Fixed #10199 -- Modified aggregate() calls to clone the base query so…
Browse files Browse the repository at this point in the history
… that the base query can be reused. Thanks to Alex Gaynor for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9819 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Feb 8, 2009
1 parent addd3df commit d4a3a4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django/db/models/query.py
Expand Up @@ -307,11 +307,13 @@ def aggregate(self, *args, **kwargs):
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):
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/aggregation_regress/models.py
Expand Up @@ -200,6 +200,12 @@ def __unicode__(self):
>>> 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()
[<Book: Artificial Intelligence: A Modern Approach>, <Book: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp>, <Book: Practical Django Projects>, <Book: Python Web Development with Django>, <Book: Sams Teach Yourself Django in 24 Hours>, <Book: The Definitive Guide to Django: Web Development Done Right>]
"""
}

Expand Down

0 comments on commit d4a3a4b

Please sign in to comment.