#11383: Admin action 'Delete selected' check only global model delete permission
-------------------------------------+-------------------------------------
Reporter: krejcik@… | Owner: (none)
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: delete permission | Triage Stage: Accepted
admin |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by rwlogel):
I just realized you can use the error message work around to solve the
issue introduced in 2.1.
{{{#!python
def has_delete_permission(self, request, obj=None):
# Only superusers can delete
if not request.user.is_superuser:
return False
if obj:
# Code to check if obj is allowed to be deleted
can_delete = .....
return can_delete
# Bulk deletes will be verified in the overridden delete_selected
method below
return True
# Override the delete_selected action
def delete_selected(self, request, queryset):
# Check if multiple items are selected to be deleted
if (
request.path.startswith('/admin/<path_to_model_being_deleted>')
and
request.POST and request.POST.get('action') == 'delete_selected'
):
# Get list of ids that are going to be deleted
id_list = request.POST.getlist(admin.helpers.ACTION_CHECKBOX_NAME)
# Use id_list to lookup object or perform some query to see if
that object can be deleted
for id in id_list:
can_delete = ....
if not can_delete:
messages.error(request, "One of the items selected can't
be deleted")
return # Don't perform delete
# Call the actual delete method
return actions.delete_selected(self, request, queryset)
delete_selected.short_description =
actions.delete_selected.short_description
actions = [delete_selected]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/11383#comment:19>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/071.7058fcaf805a566e21fc78bd7c6fdad9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.