Wagtail ModelAdmin只读 [英] Wagtail ModelAdmin read only

查看:110
本文介绍了Wagtail ModelAdmin只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Wagtails Modeladmin:

Using Wagtails Modeladmin:

是否有任何方法可以禁用编辑&删除仅留下检查视图的选项?

我能想到的一种可能的方法是扩展模板,删除编辑&删除按钮,然后以某种方式禁用编辑和删除视图.

A possible approach that I can think of, is extending the template, removing the edit & delete buttons and then somehow disable the edit and delete view.

有没有更清洁的方法?

感谢Loic的回答,我可以弄清楚.

Thanks to Loic answer I could figure out.

PermissionHelper 源代码有助于找出正确的覆盖方法.

The PermissionHelper source code was also very helpful to figure out the correct method to override.

仅显示检查视图的完整答案

class ValidationPermissionHelper(PermissionHelper):
    def user_can_list(self, user):
        return True  
    def user_can_create(self, user):
        return False
    def user_can_edit_obj(self, user, obj):
        return False
    def user_can_delete_obj(self, user, obj):
        return False

class ValidationAdmin(ModelAdmin):
    model = Validation
    permission_helper_class = ValidationPermissionHelper
    inspect_view_enabled = True
    [...]

推荐答案

遗憾的是,您需要

Sadly, you need at least one of the add, change or delete permission on that model (set within the roles) for it to show up.

解决方法是提供一个自定义权限帮助程序类添加到您的ModelAdmin并始终允许列出(并且仍然允许在角色内设置添加/更改/删除):

The way around that is to provide a custom permission helper class to your ModelAdmin and always allow listing (and still allow add/change/delete to be set within the roles):

class MyPermissionHelper(wagtail.contrib.modeladmin.helpers.PermissionHelper):
    def user_can_list(self, user):
        return True  # Or any logic related to the user.

class MyModelAdmin(wagtail.contrib.modeladmin.options.ModelAdmin):
    model = MyModel
    permission_helper_class = MyPermissionHelper

modeladmin_register(wagtail.contrib.modeladmin.options.MyModelAdmin)

这篇关于Wagtail ModelAdmin只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆