gpt4 book ai didi

python - class UserAdmin(admin.ModelAdmin) 重写多删除的删除功能

转载 作者:行者123 更新时间:2023-12-04 16:10:44 30 4
gpt4 key购买 nike

我正在为 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)

最佳答案

这将是 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)

关于python - class UserAdmin(admin.ModelAdmin) 重写多删除的删除功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42027929/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com