mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #33191 -- Avoided clearing cached reference when saving child after parent.
Thanks Barry Johnson for the report.
This commit is contained in:
parent
f1e0fc645b
commit
1058fc7023
@ -1100,9 +1100,9 @@ class Model(metaclass=ModelBase):
|
||||
"related object '%s'." % (operation_name, field.name)
|
||||
)
|
||||
elif getattr(self, field.attname) in field.empty_values:
|
||||
# Use pk from related object if it has been saved after
|
||||
# an assignment.
|
||||
setattr(self, field.attname, obj.pk)
|
||||
# Set related object if it has been saved after an
|
||||
# assignment.
|
||||
setattr(self, field.name, obj)
|
||||
# If the relationship's pk/to_field was changed, clear the
|
||||
# cached relationship.
|
||||
if getattr(obj, field.target_field.attname) != getattr(
|
||||
|
@ -654,6 +654,16 @@ class ManyToOneTests(TestCase):
|
||||
self.assertIsNot(c.parent, p)
|
||||
self.assertEqual(c.parent, p)
|
||||
|
||||
def test_save_parent_after_assign(self):
|
||||
category = Category(name="cats")
|
||||
record = Record(category=category)
|
||||
category.save()
|
||||
record.save()
|
||||
category.name = "dogs"
|
||||
with self.assertNumQueries(0):
|
||||
self.assertEqual(category.id, record.category_id)
|
||||
self.assertEqual(category.name, record.category.name)
|
||||
|
||||
def test_save_nullable_fk_after_parent(self):
|
||||
parent = Parent()
|
||||
child = ChildNullableParent(parent=parent)
|
||||
|
Loading…
Reference in New Issue
Block a user