mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #25296 -- Prevented model related object cache pollution when create() fails due to an unsaved object.
This commit is contained in:
parent
c21410aeeb
commit
f5a33e4840
@ -643,6 +643,9 @@ class Model(six.with_metaclass(ModelBase)):
|
|||||||
# constraints aren't supported by the database, there's the
|
# constraints aren't supported by the database, there's the
|
||||||
# unavoidable risk of data corruption.
|
# unavoidable risk of data corruption.
|
||||||
if obj and obj.pk is None:
|
if obj and obj.pk is None:
|
||||||
|
# Remove the object from a related instance cache.
|
||||||
|
if not field.remote_field.multiple:
|
||||||
|
delattr(obj, field.remote_field.get_cache_name())
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"save() prohibited to prevent data loss due to "
|
"save() prohibited to prevent data loss due to "
|
||||||
"unsaved related object '%s'." % field.name
|
"unsaved related object '%s'." % field.name
|
||||||
|
@ -135,9 +135,14 @@ class OneToOneTests(TestCase):
|
|||||||
should raise an exception.
|
should raise an exception.
|
||||||
"""
|
"""
|
||||||
place = Place(name='User', address='London')
|
place = Place(name='User', address='London')
|
||||||
|
with self.assertRaises(Restaurant.DoesNotExist):
|
||||||
|
place.restaurant
|
||||||
msg = "save() prohibited to prevent data loss due to unsaved related object 'place'."
|
msg = "save() prohibited to prevent data loss due to unsaved related object 'place'."
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
Restaurant.objects.create(place=place, serves_hot_dogs=True, serves_pizza=False)
|
Restaurant.objects.create(place=place, serves_hot_dogs=True, serves_pizza=False)
|
||||||
|
# place should not cache restaurant
|
||||||
|
with self.assertRaises(Restaurant.DoesNotExist):
|
||||||
|
place.restaurant
|
||||||
|
|
||||||
def test_reverse_relationship_cache_cascade(self):
|
def test_reverse_relationship_cache_cascade(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user