django 管理员覆盖 delete_model 不使用批量删除
django admin overriding delete_model not working with bulk delete
我想为特定模型实例禁用删除
我在 ModelAdmin
中覆盖了 delete_model
如下:
def delete_model(self, request, client):
if client.schema_name == 'public':
messages.set_level(request, messages.ERROR)
messages.error(request, 'Suppression interdite pour la racine')
else:
super().delete_model(request, client)
当我在更改视图上单击删除按钮时有效
但不能使用批量删除,因为实例会在没有阻止的情况下被删除
我该如何解决这个问题?我还意识到 delete_model
没有被批量删除调用,这有点奇怪。
作为批量删除管理员操作documentation 已经警告您
The “delete selected objects” action uses QuerySet.delete() for
efficiency reasons, which has an important caveat: your model’s
delete() method will not be called.
If you wish to override this behavior, you can override
ModelAdmin.delete_queryset() or write a custom action which does
deletion in your preferred manner – for example, by calling
Model.delete() for each of the selected items.
For more background on bulk deletion, see the documentation on object
deletion.
我想为特定模型实例禁用删除
我在 ModelAdmin
中覆盖了 delete_model
如下:
def delete_model(self, request, client):
if client.schema_name == 'public':
messages.set_level(request, messages.ERROR)
messages.error(request, 'Suppression interdite pour la racine')
else:
super().delete_model(request, client)
当我在更改视图上单击删除按钮时有效
但不能使用批量删除,因为实例会在没有阻止的情况下被删除
我该如何解决这个问题?我还意识到 delete_model
没有被批量删除调用,这有点奇怪。
作为批量删除管理员操作documentation 已经警告您
The “delete selected objects” action uses QuerySet.delete() for efficiency reasons, which has an important caveat: your model’s delete() method will not be called.
If you wish to override this behavior, you can override ModelAdmin.delete_queryset() or write a custom action which does deletion in your preferred manner – for example, by calling Model.delete() for each of the selected items.
For more background on bulk deletion, see the documentation on object deletion.