Ticket #21734: django-admin-protectederror.patch

File django-admin-protectederror.patch, 1.5 KB (added by sander@…, 16 months ago)

Patch to show error message in admin on ProtectedError

  • django/contrib/admin/actions.py

    diff --git a/django/contrib/admin/actions.py b/django/contrib/admin/actions.py
    index d215aa6..93c91df 100644
    a b from django.db import router 
    1010from django.template.response import TemplateResponse
    1111from django.utils.encoding import force_text
    1212from django.utils.translation import ugettext_lazy, ugettext as _
     13from django.db.models.deletion import ProtectedError
    1314
    1415
    1516def delete_selected(modeladmin, request, queryset):
    def delete_selected(modeladmin, request, queryset): 
    4647            for obj in queryset:
    4748                obj_display = force_text(obj)
    4849                modeladmin.log_deletion(request, obj, obj_display)
    49             queryset.delete()
    50             modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
    51                 "count": n, "items": model_ngettext(modeladmin.opts, n)
    52             }, messages.SUCCESS)
     50            try:
     51                queryset.delete()
     52                modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
     53                    "count": n, "items": model_ngettext(modeladmin.opts, n)
     54                }, messages.SUCCESS)
     55            except ProtectedError, e:
     56                modeladmin.message_user(request, _("Could not delete %(items)s: %(msg)s.") % {
     57                    "items": model_ngettext(modeladmin.opts, n), "msg": e.args[0]
     58                }, messages.ERROR)
     59
    5360        # Return None to display the change list page again.
    5461        return None
    5562
Back to Top