You can use the delete_queryset which is coming from Django 2.1 onward for bulk delete objects and the delete_model for single delete. Both methods will handle something before deleting the object.
ModelAdmin.delete_queryset(request, queryset)
This is the explanation about delete_queryset in release note of Django 2.1.
The delete_queryset() method is given the HttpRequest and a QuerySet of objects to be deleted. Override this method to customize the deletion process for the “delete selected objects”
Let's look at what delete_queryset does, you can override admin.ModelAdmin class in this way by including delete_queryset function. Here you'll get list of object(s), queryset.delete()
mean delete all the object(s) at once or you can add a loop to delete one by one.
def delete_queryset(self, request, queryset):
print('==========================delete_queryset==========================')
print(queryset)
"""
you can do anything here BEFORE deleting the object(s)
"""
queryset.delete()
"""
you can do anything here AFTER deleting the object(s)
"""
print('==========================delete_queryset==========================')
So I'm going to delete 5 objects from "select window" and here is those 5 objects.
Then you'll redirect to the confirmation page like this,
Keep it mind about "Yes, I'm sure" button and I'll explain it later. When you click that button you will see the below image after removing those 5 objects.
This is the terminal output,
So you'll get those 5 objects as a list of QuerySet and before deleting you can do anything what ever you want in the comment area.
ModelAdmin.delete_model(request, obj)
This is the explanation about delete_model.
The delete_model method is given the HttpRequest and a model instance. Overriding this method allows doing pre- or post-delete operations. Call super().delete_model() to delete the object using Model.delete().
Let's look at what delete_model does, you can override admin.ModelAdmin class in this way by including delete_model function.
actions = ['delete_model']
def delete_model(self, request, obj):
print('============================delete_model============================')
print(obj)
"""
you can do anything here BEFORE deleting the object
"""
obj.delete()
"""
you can do anything here AFTER deleting the object
"""
print('============================delete_model============================')
I just click my 6th object to delete from the "change window".
There is another Delete button, when you click it you'll see the window which we saw earlier.
Click "Yes, I'm sure" button to delete the single object. You'll see the following window with the notification of that deleted object.
This is the terminal output,
So you'll get selected object as a single of QuerySet and before deleting you can do anything what ever you want in the comment area.
The final conclusion is you can handle the delete event by clicking "Yes, I'm sure" button in "select window" or "change window" in Django Admin Site using delete_queryset and delete_model. In this way we don't need to handle such a signals like django.db.models.signals.pre_delete or django.db.models.signals.post_delete.
Here is the full code,
from django.contrib import admin
from . import models
class AdminInfo(admin.ModelAdmin):
model = models.AdminInfo
actions = ['delete_model']
def delete_queryset(self, request, queryset):
print('========================delete_queryset========================')
print(queryset)
"""
you can do anything here BEFORE deleting the object(s)
"""
queryset.delete()
"""
you can do anything here AFTER deleting the object(s)
"""
print('========================delete_queryset========================')
def delete_model(self, request, obj):
print('==========================delete_model==========================')
print(obj)
"""
you can do anything here BEFORE deleting the object
"""
obj.delete()
"""
you can do anything here AFTER deleting the object
"""
print('==========================delete_model==========================')
admin.site.register(models.AdminInfo, AdminInfo)