I'm using the default User class for the django authentication system.
I've overrided the delete_model function on the UserAdmin class as below to execute some stuff when deleting my user. It works fine when I go to the user on the admin page, and then click on the delete button. My problem it's when I seletect one or multiple user on the UserAdmin page, it looks like this is not the delete_model function which is called. What should I do to fix it?
from django.contrib import admin
#create a custom model admin which allow to search on the email field
class UserAdmin(admin.ModelAdmin):
#override the delete method to unset the send email to the deleted_user
def delete_model(UserAdmin, request, obj):
#### do something ####
obj.delete()
#unregister the basic User, and register the new User ModelAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
This would be the solution 1 described by iklinac:
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.admin.actions import delete_selected as django_delete_selected
class UserAdmin(DjangoUserAdmin):
actions = ['delete_selected']
def delete_model(modeladmin, request, obj):
# do something with the user instance
obj.delete()
def delete_selected(modeladmin, request, queryset):
# do something with the users in the queryset
return django_delete_selected(modeladmin, request, queryset)
delete_selected.short_description = django_delete_selected.short_description
admin.site.unregister(User)
admin.site.register(User, UserAdmin)