diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ce732b1046d..4735fdabbb5 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -196,7 +196,7 @@ class BaseQuery(object): obj.distinct = self.distinct obj.select_related = self.select_related obj.related_select_cols = [] - obj.aggregates = self.aggregates.copy() + obj.aggregates = deepcopy(self.aggregates) if self.aggregate_select_mask is None: obj.aggregate_select_mask = None else: @@ -2366,4 +2366,3 @@ def add_to_dict(data, key, value): data[key].add(value) else: data[key] = set([value]) - diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py index 275ea073901..2dfa1818fce 100644 --- a/tests/regressiontests/aggregation_regress/models.py +++ b/tests/regressiontests/aggregation_regress/models.py @@ -259,6 +259,23 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta >>> Book.objects.annotate(Count('publisher')).values('publisher').count() 6 +>>> publishers = Publisher.objects.filter(id__in=(1,2)) +>>> publishers +[, ] + +>>> publishers = publishers.annotate(n_books=models.Count('book')) +>>> publishers[0].n_books +2 + +>>> publishers +[, ] + +>>> books = Book.objects.filter(publisher__in=publishers) +>>> books +[, , ] + +>>> publishers +[, ] """ } @@ -307,4 +324,3 @@ if settings.DATABASE_ENGINE != 'sqlite3': """ -