Custom Django 'delete' action not firing from admin list view

417 Views Asked by At

I have a 'notes' model in my Django app where I've built some custom delete actions:

class Note(models.Model):
    [MODEL FIELDS HERE]

    def delete(self, *args, **kwargs):
        [STUFF HEPPENS HERE]
        super(Note, self).delete()

If I click into the individual note from the Django admin and hit the 'Delete' button, the custom delete actions work just fine, but if I check the box next to the note in the list view and choose 'Delete selected notes' from the dropdown, they do not.

What am I missing here?

2

There are 2 best solutions below

0
campegg On

See @Brian Destura's answer in the comments above.

0
knaperek On

delete selected is a bulk action and for efficiency reasons Django deletes the whole queryset at once, not calling the delete() method on each model instance.

If you need to make sure it does get called, e.g. you have some custom handling there that should not be skipped, just override the delete_queryset method on your ModelAdmin subclass like this:

# content of admin.py in your Django app

from django.contrib import admin

class MyModelAdmin(admin.ModelAdmin):
    def delete_queryset(self, request, queryset):
        for obj in queryset.all():
            obj.delete()