summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAkis Kesoglou <akis@radial.gr>2014-08-29 17:01:21 +0300
committerTim Graham <timograham@gmail.com>2014-08-30 07:14:44 -0400
commit6613ea6e3ff2597c04db35ef885562e95c1ef012 (patch)
treee9b921cb98e2115cfa13eca03a8dfb2d636fc1e4 /tests
parent66757fee7e921ad4c35e0b3f80c25e026100b31c (diff)
Fixed #23370 -- defer() + select_related() crashed with inherited models.
Diffstat (limited to 'tests')
-rw-r--r--tests/model_inheritance/tests.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/model_inheritance/tests.py b/tests/model_inheritance/tests.py
index 0af96b820d..77210174a0 100644
--- a/tests/model_inheritance/tests.py
+++ b/tests/model_inheritance/tests.py
@@ -255,6 +255,43 @@ class ModelInheritanceTests(TestCase):
1, lambda: ItalianRestaurant.objects.select_related("chef")[0].chef
)
+ def test_select_related_defer(self):
+ """
+ #23370 - Should be able to defer child fields when using
+ select_related() from parent to child.
+ """
+ Restaurant.objects.create(
+ name="Demon Dogs",
+ address="944 W. Fullerton",
+ serves_hot_dogs=True,
+ serves_pizza=False,
+ rating=2,
+ )
+ ItalianRestaurant.objects.create(
+ name="Ristorante Miron",
+ address="1234 W. Ash",
+ serves_hot_dogs=False,
+ serves_pizza=False,
+ serves_gnocchi=True,
+ rating=4,
+ )
+
+ qs = (Restaurant.objects
+ .select_related("italianrestaurant")
+ .defer("italianrestaurant__serves_gnocchi")
+ .order_by("rating"))
+
+ # Test that the field was actually defered
+ with self.assertNumQueries(2):
+ objs = list(qs.all())
+ self.assertTrue(objs[1].italianrestaurant.serves_gnocchi)
+
+ # Test that model fields where assigned correct values
+ self.assertEqual(qs[0].name, 'Demon Dogs')
+ self.assertEqual(qs[0].rating, 2)
+ self.assertEqual(qs[1].italianrestaurant.name, 'Ristorante Miron')
+ self.assertEqual(qs[1].italianrestaurant.rating, 4)
+
def test_mixin_init(self):
m = MixinModel()
self.assertEqual(m.other_attr, 1)