-
-
Save rudyryk/4190318 to your computer and use it in GitHub Desktop.
from django.core.exceptions import PermissionDenied | |
from django.contrib import admin | |
from django.contrib.admin.actions import delete_selected as delete_selected_ | |
def delete_selected(modeladmin, request, queryset): | |
if not modeladmin.has_delete_permission(request): | |
raise PermissionDenied | |
if request.POST.get('post'): | |
for obj in queryset: | |
obj.delete() | |
else: | |
return delete_selected_(modeladmin, request, queryset) | |
delete_selected.short_description = "Delete selected objects" | |
class MyModelAdmin(admin.ModelAdmin): | |
actions = [delete_selected] |
Thanks for the answers.
@boehlke your code has an error:
has_delete_permission
can not pass queryset
argument, otherwise it will raise TypeError
@boehike Look at the django.contrib.admin.actions.delete_selected
. The 'post'
parameter is set in the confirmation page (that confirms the deletion of related objects)
you are not overriding, you are creating a custom method and replacing the default function.
Overriding will keep all functionality of default function and only changed the part you wanted to change.
This one for example will remove the intermediate step of warning (this action will delete
and will replace specific short description should be Delete selected <> to a generic delete selected objects
How to override that in Django 2?
Hi,
I have got the problem with my exercise where I have to block ability superuser to delete other superuser and himself.
admin.py
`def delete_selected(modeladmin, request, queryset):
if not modeladmin.has_delete_permission(request):
raise PermissionDenied
if request.POST.get('post'):
for obj in queryset:
if not obj.is_superuser:
obj.delete()
else:
return delete_selected_(modeladmin, request, queryset)
delete_selected.short_description = "Delete selected objects"
and url.py
path("admin/auth/user/",delete_selected), `
Great gist, thanks.
I don't understand the "if request.POST.get('post'):" condition. Where does the 'post' POST parameter come from. I use this gist without this condition like that: