Code

Ticket #15523: deffer_bug.diff

File deffer_bug.diff, 2.3 KB (added by milosu, 3 years ago)
Line 
1Index: django/db/models/sql/compiler.py
2===================================================================
3--- django/db/models/sql/compiler.py    (revision 5060)
4+++ django/db/models/sql/compiler.py    (working copy)
5@@ -156,7 +156,7 @@
6                 if isinstance(col, (list, tuple)):
7                     alias, column = col
8                     table = self.query.alias_map[alias][TABLE_NAME]
9-                    if table in only_load and col not in only_load[table]:
10+                    if table in only_load and column not in only_load[table]:
11                         continue
12                     r = '%s.%s' % (qn(alias), qn(column))
13                     if with_aliases:
14Index: django/db/models/query.py
15===================================================================
16--- django/db/models/query.py   (revision 5060)
17+++ django/db/models/query.py   (working copy)
18@@ -264,6 +264,8 @@
19                     # Model wasn't explicitly listed in the only_load table
20                     # Therefore, we need to load all fields from this model
21                     load_fields.append(field.name)
22+            # fix aggregate start based on the new number of fields
23+            aggregate_start = index_start + len(load_fields)
24 
25         skip = None
26         if load_fields and not fill_cache:
27Index: tests/regressiontests/defer_regress/tests.py
28===================================================================
29--- tests/regressiontests/defer_regress/tests.py        (revision 5060)
30+++ tests/regressiontests/defer_regress/tests.py        (working copy)
31@@ -1,5 +1,6 @@
32 from django.test import TestCase
33-from models import ResolveThis
34+from django.db.models import Count
35+from models import ResolveThis, Child, Leaf
36 
37 class DeferRegressionTest(TestCase):
38     def test_resolve_columns(self):
39@@ -7,3 +8,11 @@
40         qs = ResolveThis.objects.defer('num')
41         self.assertEqual(1, qs.count())
42         self.assertEqual('Foobar', qs[0].name)
43+    def test_annotations(self):
44+        ch = Child.objects.create(name='first', value=10)
45+       l = Leaf.objects.create(name='leaf', child = ch)
46+       qset = Leaf.objects.all().annotate(max_cnt = Count('child')).only('id', 'name')
47+       self.assertTrue(len(qset) == 1)
48+       self.assertTrue(qset[0].name == u'leaf')
49+       self.assertTrue(qset[0].id == 1)
50+       self.assertTrue(qset[0].max_cnt == 1)