我使用django身份验证系统的默认用户类。
我已经覆盖UserAdmin类上的delete_model函数,如下所示,在删除我的用户时执行一些操作。当我在管理页面上转到用户,然后单击删除按钮时,它工作正常。我的问题是当我在UserAdmin页面上选择一个或多个用户时,看起来这不是被调用的delete_model函数。我该怎么做才能解决它?
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)
答案 0 :(得分:0)
删除所选项是由名为 delete_selected
的Django函数完成的from django.contrib.admin.actions import delete_selected
有两种解决方法
禁用原始delete_selected并创建您自己将添加到管理操作中的
覆盖UserModels queryset / manager delete()方法
答案 1 :(得分:0)
这将是iklinac描述的解决方案1:
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)