在 Django Admin 中如何禁用删除链接

发布于 2024-09-29 08:41:27 字数 64 浏览 2 评论 0原文

我已设法禁用“删除所选”操作。简单的。

但用户仍然可以单击某个项目,然后底部会出现红色的删除链接。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独﹏钓一江月 2024-10-06 08:41:27

简单的 :)

class DeleteNotAllowedModelAdmin(admin.ModelAdmin):
    # Other stuff here
    def has_delete_permission(self, request, obj=None):
        return False

Simple :)

class DeleteNotAllowedModelAdmin(admin.ModelAdmin):
    # Other stuff here
    def has_delete_permission(self, request, obj=None):
        return False
泪之魂 2024-10-06 08:41:27

如果您想禁用非自定义的特定项,请执行此操作。在 django 1.6.6 中,我必须扩展 get_actions 并定义 has_delete_permissionhas_delete_permission 解决方案不会为我删除下拉列表中的操作:

class MyModelAdmin(admin.ModelAdmin):

    ....

    def get_actions(self, request):
        #Disable delete
        actions = super(MyModelAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

    def has_delete_permission(self, request, obj=None):
        #Disable delete
        return False

不将其包含在 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 define has_delete_permission. The has_delete_permission solution does not get rid of the action from the dropdown for me:

class MyModelAdmin(admin.ModelAdmin):

    ....

    def get_actions(self, request):
        #Disable delete
        actions = super(MyModelAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

    def has_delete_permission(self, request, obj=None):
        #Disable delete
        return False

Not including it in actions = ['your_custom_action'], only works for the custom actions (defs) you have defined for that model. The solution AdminSite.disable_action('delete_selected'), disables it for all models, so you would have to explicitly include them later per each modelAdmin

凉墨 2024-10-06 08:41:27

只需禁用该用户或其所属组的 yourapp.delete_yourmodel 权限即可。

Simply disable the yourapp.delete_yourmodel permission for that user or the group to which (s)he belongs.

打小就很酷 2024-10-06 08:41:27

那么您可能正在使用:

AdminSite.disable_action('delete_selected')

为了进一步控制,只需实现您自己的管理并将其操作设置为您需要的任何内容:

class MyModelAdmin(admin.ModelAdmin):
    actions = ['whatever', 'actions']

参考:http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-行动

Well you probably are using:

AdminSite.disable_action('delete_selected')

For further control just implement your own admin and set its actions to whatever you need:

class MyModelAdmin(admin.ModelAdmin):
    actions = ['whatever', 'actions']

Reference: http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-a-site-wide-action

圈圈圆圆圈圈 2024-10-06 08:41:27

这里的解决方案已经很好了,但我更喜欢将其作为可重复使用的 mixin,如下所示:

class NoDeleteAdminMixin:
    def has_delete_permission(self, request, obj=None):
        return False

您可以在所有想要防止删除的管理员中使用它,如下所示:

class MyAdmin(NoDeleteAdminMixin, ModelAdmin):
    ...

The solutions here are already nice, but I prefer to have it as a reusable mixin, like this:

class NoDeleteAdminMixin:
    def has_delete_permission(self, request, obj=None):
        return False

You can use this in all your admins where you want to prevent deletion like this:

class MyAdmin(NoDeleteAdminMixin, ModelAdmin):
    ...
苏别ゝ 2024-10-06 08:41:27

admin.site.disable_action('delete_selected')

来自 文档

admin.site.disable_action('delete_selected')

From the docs

深者入戏 2024-10-06 08:41:27

这是很古老的,但仍然可以帮助某人。

假设OP的

...用户仍然可以单击某个项目,然后底部会出现红色的删除链接。

指“更改”视图中的红色按钮。可以通过扩展 ModelAdmin.change_view 方法来删​​除此按钮,如下所示:

def change_view(self, request, object_id=None, form_url='', extra_context=None):
    return super().change_view(request, object_id, form_url,
                               extra_context=dict(show_delete=False))

您可以对 show_saveshow_save_and_continue 执行相同的操作。更多信息和替代方案此处

另请注意,从版本 2.1 开始,Django 有一个单独的 has_view_permission (docs),可能是更好的选择,具体取决于您的用例。

This is very old, but still, it may help someone.

Assuming that OP's

... user can still click on an item and then there's the red Delete link at the bottom.

refers to the red button in the "change" view. This button can be removed by extending the ModelAdmin.change_view method as follows:

def change_view(self, request, object_id=None, form_url='', extra_context=None):
    return super().change_view(request, object_id, form_url,
                               extra_context=dict(show_delete=False))

You can do the same with show_save, and show_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文