管理列表删除和模型delete()方法

发布于 2024-10-21 02:22:56 字数 179 浏览 2 评论 0原文

我在我的模型中定义了 delete() 方法。我用它来清除缓存和删除拇指。当我在管理中删除一个元素时,在其详细信息(编辑模式)中,缓存将被清除,所有拇指都将被删除。 但是,当我想一次删除多个元素时 - 检查元素列表上的复选框 - 元素被删除,但 cahce 和缩略图没有被删除,所以我的模型删除方法没有创建:/

我该如何修复它?

I have defined delete() method in my model. I use it to clear cache and delete thumbs. When I delete an element in admin, in it's details (edit mode) cache is cleared and all thumbs are deleted.
But, when i want to delete several elements at once - checking checkboxes on the elements list - elements are deleted, but cahce and thumbnails aren't deleted, so my model delete method isn't made:/

How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烧了回忆取暖 2024-10-28 02:22:56

它位于文档的第一部分。出于效率原因,Django Admin 对查询集对象调用delete。这完全绕过了每个实例的删除方法。阅读文档。

如果您希望覆盖此行为,只需编写一个自定义操作,以您喜欢的方式完成删除 - 例如,通过为每个选定的项目调用 Model.delete() 。

It's in the very first section of the documentation. Django Admin calls delete on the queryset object for efficiency reasons. That totally bypasses each instances delete method. Read the docs.

If you wish to override this behavior, simply write a custom action which accomplishes deletion in your preferred manner – for example, by calling Model.delete() for each of the selected items.

我的鱼塘能养鲲 2024-10-28 02:22:56

如文档中所述,您需要重写 delete selected 操作:

def delete_selected(modeladmin, request, queryset):
    for element in queryset:
        element.delete()
delete_selected.short_description = "Delete selected elements"

class ElementAdmin(admin.ModelAdmin):
    actions = [delete_selected]

admin.site.register(Element, ElementAdmin)

As described in the documentation, you need to override the delete selected action:

def delete_selected(modeladmin, request, queryset):
    for element in queryset:
        element.delete()
delete_selected.short_description = "Delete selected elements"

class ElementAdmin(admin.ModelAdmin):
    actions = [delete_selected]

admin.site.register(Element, ElementAdmin)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文