About the Delete Admin Action
The delete()
method of a model is not called when you trigger the "delete" admin action. The records are deleted at the QuerySet level.
When you need to do some pre- or post-processing for each model instance with the "delete" admin action, you can overwrite the delete_queryset()
method of the model admin:
admin.py
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
def delete_queryset(self, request, queryset):
for obj in queryset:
obj.delete()
models.py
class MyModel(models.Model):
def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
# ...
Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.