Ticket #10199: aggregation-clone.diff

File aggregation-clone.diff, 1.9 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index e33a858..876968d 100644
    a b class QuerySet(object):  
    306306        """
    307307        for arg in args:
    308308            kwargs[arg.default_alias] = arg
     309       
     310        query = self.query.clone()
    309311
    310312        for (alias, aggregate_expr) in kwargs.items():
    311             self.query.add_aggregate(aggregate_expr, self.model, alias,
     313            query.add_aggregate(aggregate_expr, self.model, alias,
    312314                is_summary=True)
    313315
    314         return self.query.get_aggregation()
     316        return query.get_aggregation()
    315317
    316318    def count(self):
    317319        """
  • tests/regressiontests/aggregation_regress/models.py

    diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
    index 24de920..6919b71 100644
    a b FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta  
    200200>>> sorted([(b.name, b.authors__age__avg, b.publisher.name, b.contact.name) for b in books])
    201201[(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')]
    202202
     203>>> books = Book.objects.all()
     204>>> _ = books.aggregate(Avg('authors__age'))
     205>>> books.all()
     206[<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>]
     207
    203208"""
    204209}
    205210
Back to Top