开发者

Admin list delete and model delete() method

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:/开发者_JS百科

How can I fix it?


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜