在 Django Admin 中如何禁用删除链接
我已设法禁用“删除所选”操作。简单的。
但用户仍然可以单击某个项目,然后底部会出现红色的删除链接。
I've managed to disable the "Delete selected" action. Easy.
But a user can still click on an item and then there's the red Delete link at the bottom.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
简单的 :)
Simple :)
如果您想禁用非自定义的特定项,请执行此操作。在 django 1.6.6 中,我必须扩展
get_actions
并定义has_delete_permission
。has_delete_permission
解决方案不会为我删除下拉列表中的操作:不将其包含在
actions = ['your_custom_action']
中,仅适用于自定义操作 (defs )您已经为该模型定义了。解决方案AdminSite.disable_action('delete_selected')
为所有模型禁用它,因此您必须稍后在每个 modelAdmin 中显式包含它们If you want to disable an specific one that isn't custom do this. In django 1.6.6 I had to extend
get_actions
plus definehas_delete_permission
. Thehas_delete_permission
solution does not get rid of the action from the dropdown for me:Not including it in
actions = ['your_custom_action']
, only works for the custom actions (defs) you have defined for that model. The solutionAdminSite.disable_action('delete_selected')
, disables it for all models, so you would have to explicitly include them later per each modelAdmin只需禁用该用户或其所属组的
yourapp.delete_yourmodel
权限即可。Simply disable the
yourapp.delete_yourmodel
permission for that user or the group to which (s)he belongs.那么您可能正在使用:
为了进一步控制,只需实现您自己的管理并将其操作设置为您需要的任何内容:
参考:http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-行动
Well you probably are using:
For further control just implement your own admin and set its actions to whatever you need:
Reference: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-action
这里的解决方案已经很好了,但我更喜欢将其作为可重复使用的 mixin,如下所示:
您可以在所有想要防止删除的管理员中使用它,如下所示:
The solutions here are already nice, but I prefer to have it as a reusable mixin, like this:
You can use this in all your admins where you want to prevent deletion like this:
admin.site.disable_action('delete_selected')
来自 文档
admin.site.disable_action('delete_selected')
From the docs
这是很古老的,但仍然可以帮助某人。
假设OP的
指“更改”视图中的红色按钮。可以通过扩展
ModelAdmin.change_view
方法来删除此按钮,如下所示:您可以对
show_save
和show_save_and_continue
执行相同的操作。更多信息和替代方案此处。另请注意,从版本 2.1 开始,Django 有一个单独的
has_view_permission
(docs),可能是更好的选择,具体取决于您的用例。This is very old, but still, it may help someone.
Assuming that OP's
refers to the red button in the "change" view. This button can be removed by extending the
ModelAdmin.change_view
method as follows:You can do the same with
show_save
, andshow_save_and_continue
. More info and alternatives here.Also note that, as of version 2.1, Django has a separate
has_view_permission
(docs), which may be a better option, depending on your use case.