Code

Ticket #10670: aggregation-copy.diff

File aggregation-copy.diff, 1.8 KB (added by Alex, 5 years ago)
Line 
1diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
2index 0583386..2ce9e29 100644
3--- a/django/db/models/sql/query.py
4+++ b/django/db/models/sql/query.py
5@@ -196,7 +196,7 @@ class BaseQuery(object):
6         obj.distinct = self.distinct
7         obj.select_related = self.select_related
8         obj.related_select_cols = []
9-        obj.aggregates = self.aggregates.copy()
10+        obj.aggregates = deepcopy(self.aggregates)
11         if self.aggregate_select_mask is None:
12             obj.aggregate_select_mask = None
13         else:
14@@ -2368,4 +2368,3 @@ def add_to_dict(data, key, value):
15         data[key].add(value)
16     else:
17         data[key] = set([value])
18-
19diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
20index 275ea07..b7af59b 100644
21--- a/tests/regressiontests/aggregation_regress/models.py
22+++ b/tests/regressiontests/aggregation_regress/models.py
23@@ -259,6 +259,25 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta
24 >>> Book.objects.annotate(Count('publisher')).values('publisher').count()
25 6
26 
27+>>> publishers = Publisher.objects.filter(id__in=(1,2))
28+>>> publishers
29+[<Publisher: Apress>, <Publisher: Sams>]
30+
31+>>> publishers = publishers.annotate(n_books=models.Count('book'))
32+>>> publishers[0].n_books
33+2
34+
35+>>> publishers
36+[<Publisher: Apress>, <Publisher: Sams>]
37+
38+>>> books = Book.objects.filter(publisher__in=publishers)
39+>>> books
40+[<Book: Practical Django Projects>, <Book: Sams Teach Yourself Django in 24 Hours>, <Book: The Definitive Guide to Django: Web Development Done Right>]
41+
42+>>> publishers._result_cache
43+
44+>>> publishers
45+[<Publisher: Apress>, <Publisher: Sams>]
46 """
47 }
48 
49@@ -307,4 +326,3 @@ if settings.DATABASE_ENGINE != 'sqlite3':
50 
51 
52 """
53-