From a8a81aae20a81e012fddc24f3ede556501af64a2 Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Sun, 27 May 2012 02:08:44 +0300 Subject: Fixed #18343 -- Cleaned up deferred model implementation Generic cleanup and dead code removal in deferred model field loading and model.__reduce__(). Also fixed an issue where if an inherited model with a parent field chain parent_ptr_id -> id would be deferred loaded, then accessing the id field caused caused a database query, even if the id field's value is already loaded in the parent_ptr_id field. --- tests/modeltests/defer/tests.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tests/modeltests/defer/tests.py') diff --git a/tests/modeltests/defer/tests.py b/tests/modeltests/defer/tests.py index 09138293af..eb09162b01 100644 --- a/tests/modeltests/defer/tests.py +++ b/tests/modeltests/defer/tests.py @@ -158,3 +158,18 @@ class DeferTests(TestCase): self.assert_delayed(child, 1) self.assertEqual(child.name, 'p1') self.assertEqual(child.value, 'xx') + + def test_defer_inheritance_pk_chaining(self): + """ + When an inherited model is fetched from the DB, its PK is also fetched. + When getting the PK of the parent model it is useful to use the already + fetched parent model PK if it happens to be available. Tests that this + is done. + """ + s1 = Secondary.objects.create(first="x1", second="y1") + bc = BigChild.objects.create(name="b1", value="foo", related=s1, + other="bar") + bc_deferred = BigChild.objects.only('name').get(pk=bc.pk) + with self.assertNumQueries(0): + bc_deferred.id + self.assertEqual(bc_deferred.pk, bc_deferred.id) -- cgit v1.3