mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -17,6 +17,10 @@ class CustomField(TestCase):
|
||||
self.assertTrue(isinstance(d.data, list))
|
||||
self.assertEqual(d.data, [1, 2, 3])
|
||||
|
||||
d = DataModel.objects.defer("data").get(pk=d.pk)
|
||||
self.assertTrue(isinstance(d.data, list))
|
||||
self.assertEqual(d.data, [1, 2, 3])
|
||||
# Refetch for save
|
||||
d = DataModel.objects.defer("data").get(pk=d.pk)
|
||||
d.save()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user