From d6bd576ee0ba72ea69b4c74d13223e5c61f63dd3 Mon Sep 17 00:00:00 2001
From: Michal Petrucha <michal.petrucha@ksp.sk>
Date: Sat, 31 Dec 2011 23:23:21 +0100
Subject: [PATCH] Fixed defer for ForeignKeys with select_related.
diff --git a/django/db/models/query.py b/django/db/models/query.py
index fd531dd..0f3c39a 100644
a
|
b
|
def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
|
1281 | 1281 | # Build the list of fields that *haven't* been requested |
1282 | 1282 | for field, model in klass._meta.get_fields_with_model(): |
1283 | 1283 | if field.name not in load_fields: |
1284 | | skip.add(field.name) |
| 1284 | skip.add(field.attname) |
1285 | 1285 | elif local_only and model is not None: |
1286 | 1286 | continue |
1287 | 1287 | else: |
diff --git a/tests/regressiontests/defer_regress/tests.py b/tests/regressiontests/defer_regress/tests.py
index 4afe39b..c1a8a68 100644
a
|
b
|
class DeferRegressionTest(TestCase):
|
139 | 139 | "Leaf", |
140 | 140 | "Leaf_Deferred_child_id_second_child_id_value", |
141 | 141 | "Leaf_Deferred_name_value", |
142 | | "Leaf_Deferred_second_child_value", |
| 142 | "Leaf_Deferred_second_child_id_value", |
143 | 143 | "Leaf_Deferred_value", |
144 | 144 | "Proxy", |
145 | 145 | "RelatedItem", |
… |
… |
class DeferRegressionTest(TestCase):
|
174 | 174 | qs = ResolveThis.objects.defer('num') |
175 | 175 | self.assertEqual(1, qs.count()) |
176 | 176 | self.assertEqual('Foobar', qs[0].name) |
| 177 | |
| 178 | def test_defer_with_select_related(self): |
| 179 | item1 = Item.objects.create(name="first", value=47) |
| 180 | item2 = Item.objects.create(name="second", value=42) |
| 181 | related = RelatedItem.objects.create(item=item1) |
| 182 | |
| 183 | obj = RelatedItem.objects.defer('item').select_related('item').get() |
| 184 | self.assertEqual(obj.item, item1) |
| 185 | self.assertEqual(obj.item_id, item1.id) |
| 186 | |
| 187 | obj.item = item2 |
| 188 | obj.save() |
| 189 | |
| 190 | obj = RelatedItem.objects.defer('item').select_related('item').get() |
| 191 | self.assertEqual(obj.item, item2) |
| 192 | self.assertEqual(obj.item_id, item2.id) |