mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #31219 -- Fixed object deletion crash for nested protected related objects.
This commit is contained in:
parent
8c0c0235b6
commit
4e8d89020c
@ -296,10 +296,7 @@ class Collector:
|
|||||||
try:
|
try:
|
||||||
field.remote_field.on_delete(self, field, sub_objs, self.using)
|
field.remote_field.on_delete(self, field, sub_objs, self.using)
|
||||||
except ProtectedError as error:
|
except ProtectedError as error:
|
||||||
key = "'%s.%s'" % (
|
key = "'%s.%s'" % (field.model.__name__, field.name)
|
||||||
error.protected_objects[0].__class__.__name__,
|
|
||||||
field.name,
|
|
||||||
)
|
|
||||||
protected_objects[key] += error.protected_objects
|
protected_objects[key] += error.protected_objects
|
||||||
if protected_objects:
|
if protected_objects:
|
||||||
raise ProtectedError(
|
raise ProtectedError(
|
||||||
|
@ -90,6 +90,17 @@ class OnDeleteTests(TestCase):
|
|||||||
with self.assertRaisesMessage(ProtectedError, msg):
|
with self.assertRaisesMessage(ProtectedError, msg):
|
||||||
a.protect.delete()
|
a.protect.delete()
|
||||||
|
|
||||||
|
def test_protect_path(self):
|
||||||
|
a = create_a('protect')
|
||||||
|
a.protect.p = P.objects.create()
|
||||||
|
a.protect.save()
|
||||||
|
msg = (
|
||||||
|
"Cannot delete some instances of model 'P' because they are "
|
||||||
|
"referenced through protected foreign keys: 'R.p'."
|
||||||
|
)
|
||||||
|
with self.assertRaisesMessage(ProtectedError, msg):
|
||||||
|
a.protect.p.delete()
|
||||||
|
|
||||||
def test_do_nothing(self):
|
def test_do_nothing(self):
|
||||||
# Testing DO_NOTHING is a bit harder: It would raise IntegrityError for a normal model,
|
# Testing DO_NOTHING is a bit harder: It would raise IntegrityError for a normal model,
|
||||||
# so we connect to pre_delete and set the fk to a known value.
|
# so we connect to pre_delete and set the fk to a known value.
|
||||||
|
Loading…
Reference in New Issue
Block a user