Skip to content

Instantly share code, notes, and snippets.

@rudyryk
Created December 2, 2012 18:24
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudyryk/4190318 to your computer and use it in GitHub Desktop.
Save rudyryk/4190318 to your computer and use it in GitHub Desktop.
Override delete_selected in Django admin
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]
@boehlke
Copy link

boehlke commented May 15, 2014

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:

def delete_selected(modeladmin, request, queryset):
    if not modeladmin.has_delete_permission(request, queryset=queryset):
        raise PermissionDenied
    for obj in queryset:
        obj.delete()

delete_selected.short_description = "Delete selected objects"

@yaojialyu
Copy link

Thanks for the answers.

@boehlke your code has an error:
has_delete_permission can not pass queryset argument, otherwise it will raise TypeError

https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_delete_permission

@DylanYoung
Copy link

DylanYoung commented Sep 22, 2016

@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)

@lmchawla
Copy link

lmchawla commented Apr 2, 2018

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

@rgcarrasqueira
Copy link

How to override that in Django 2?

@AleksanderRadziszewski
Copy link

AleksanderRadziszewski commented Jul 3, 2020

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), `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment