1. 自定义管理接口
1.1 删除模型
从管理界面删除时,删除实际上是应用在整个QuerySet上,而不是在对象级别上。因此,如果需要对管理界面的删除操作执行一些对象级别的修改,可以删除默认的“delete_selected”操作,并执行自定义删除操作。如:
class SomeModelAdmin(admin.ModelAdmin):
actions = ['custom_delete']
def custom_delete(self, request, queryset):
for object in queryset:
perform_some_action(object)
object.delete()
custom_delete.short_description = "Delete selected items"
def get_actions(self, request):
actions = super(SomeModelAdmin, self).get_actions(request)
del actions['delete_selected']
return actions