gpt4 book ai didi

django - 有没有办法覆盖 ModelAdmin 中的 delete_selected 方法但保持确认?

转载 作者:行者123 更新时间:2023-12-03 18:40:45 32 4
gpt4 key购买 nike

我有:

class Person(admin.ModelAdmin):
actions = ['delete_selected']
def delete_selected(modeladmin, request, queryset):
# Show confirmation page.
for obj in queryset:
obj.custom_delete()

我在那里留下的评论是我挣扎的地方。在执行自定义删除之前,我仍然想显示确认页面。

最佳答案

简短回答 :您应该覆盖 delete_queryset [Django-doc] ,因为这封装了删除对象的真实逻辑。

您不应覆盖 delete_selected 。这个 Action 是 defined like [GitHub] :

def delete_selected(modeladmin, request, queryset):

# ...

# Populate deletable_objects, a data structure of all related objects that
# will also be deleted.
deletable_objects, model_count, perms_needed, protected = modeladmin.get_deleted_objects(queryset, request)

# The user has already confirmed the deletion.
# Do the deletion and return None to display the change list view again.
if request.POST.get('post') and not protected:
if perms_needed:
raise PermissionDenied
n = queryset.count()
if n:
for obj in queryset:
obj_display = str(obj)
modeladmin.log_deletion(request, obj, obj_display)
modeladmin.delete_queryset(request, queryset)
modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
"count": n, "items": model_ngettext(modeladmin.opts, n)
}, messages.SUCCESS)
# Return None to display the change list page again.
return None

# ...

context = {
# ...
}

request.current_app = modeladmin.admin_site.name

# Display the confirmation page
return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [
"admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.model_name),
"admin/%s/delete_selected_confirmation.html" % app_label,
"admin/delete_selected_confirmation.html"
], context)


delete_selected.allowed_permissions = ('delete',)
delete_selected.short_description = gettext_lazy("Delete selected %(verbose_name_plural)s")


这里的关键部分是此操作将执行适当的检查,但删除本身是通过调用完成的:

            modeladmin.delete_queryset(request, queryset)


因此,使用以下命令覆盖 delete_queryset 就足够了:
class PersonAdmin(admin.ModelAdmin):

actions = ['delete_selected']

def delete_queryset(self, request, queryset):
for obj in queryset:
obj.custom_delete()

一个 ModelAdmin 有一个 standard implementation for delete_queryset [GitHub] :

class ModelAdmin(BaseModelAdmin):

# ...

def delete_queryset(self, request, queryset):
"""Given a queryset, delete it from the database."""
queryset.delete()

关于django - 有没有办法覆盖 ModelAdmin 中的 delete_selected 方法但保持确认?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56450998/

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