1. 自定义管理接口

1.1 删除模型

从管理界面删除时,删除实际上是应用在整个QuerySet上,而不是在对象级别上。因此,如果需要对管理界面的删除操作执行一些对象级别的修改,可以删除默认的“delete_selected”操作,并执行自定义删除操作。如:

  1. class SomeModelAdmin(admin.ModelAdmin):
  2. actions = ['custom_delete']
  3. def custom_delete(self, request, queryset):
  4. for object in queryset:
  5. perform_some_action(object)
  6. object.delete()
  7. custom_delete.short_description = "Delete selected items"
  8. def get_actions(self, request):
  9. actions = super(SomeModelAdmin, self).get_actions(request)
  10. del actions['delete_selected']
  11. return actions