mirror of
https://github.com/django/django.git
synced 2025-10-26 15:16:09 +00:00
Fixed #14672 - Added admin handling for on_delete=PROTECT. Thanks to jtiai for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15249 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -7,17 +7,27 @@ from django.utils.datastructures import SortedDict
|
||||
from django.utils.functional import wraps
|
||||
|
||||
|
||||
class ProtectedError(IntegrityError):
|
||||
def __init__(self, msg, protected_objects):
|
||||
self.protected_objects = protected_objects
|
||||
super(ProtectedError, self).__init__(msg, protected_objects)
|
||||
|
||||
|
||||
def CASCADE(collector, field, sub_objs, using):
|
||||
collector.collect(sub_objs, source=field.rel.to,
|
||||
source_attr=field.name, nullable=field.null)
|
||||
if field.null and not connections[using].features.can_defer_constraint_checks:
|
||||
collector.add_field_update(field, None, sub_objs)
|
||||
|
||||
|
||||
def PROTECT(collector, field, sub_objs, using):
|
||||
raise IntegrityError("Cannot delete some instances of model '%s' because "
|
||||
raise ProtectedError("Cannot delete some instances of model '%s' because "
|
||||
"they are referenced through a protected foreign key: '%s.%s'" % (
|
||||
field.rel.to.__name__, sub_objs[0].__class__.__name__, field.name
|
||||
))
|
||||
),
|
||||
sub_objs
|
||||
)
|
||||
|
||||
|
||||
def SET(value):
|
||||
if callable(value):
|
||||
@@ -28,14 +38,18 @@ def SET(value):
|
||||
collector.add_field_update(field, value, sub_objs)
|
||||
return set_on_delete
|
||||
|
||||
|
||||
SET_NULL = SET(None)
|
||||
|
||||
|
||||
def SET_DEFAULT(collector, field, sub_objs, using):
|
||||
collector.add_field_update(field, field.get_default(), sub_objs)
|
||||
|
||||
|
||||
def DO_NOTHING(collector, field, sub_objs, using):
|
||||
pass
|
||||
|
||||
|
||||
def force_managed(func):
|
||||
@wraps(func)
|
||||
def decorated(self, *args, **kwargs):
|
||||
@@ -55,6 +69,7 @@ def force_managed(func):
|
||||
transaction.leave_transaction_management(using=self.using)
|
||||
return decorated
|
||||
|
||||
|
||||
class Collector(object):
|
||||
def __init__(self, using):
|
||||
self.using = using
|
||||
|
||||
Reference in New Issue
Block a user