Wagtail ModelAdmin read only

China☆狼群 提交于 2019-12-22 09:38:18

问题


Using Wagtails Modeladmin:

Is there any way to disable edit & delete options leaving only the inspect view?

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.

Is there any cleaner approach?


EDIT: Thanks to Loic answer I could figure out.

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

Complete answer for only showing inspect view

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
    [...]

回答1:


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.

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)


来源:https://stackoverflow.com/questions/42820895/wagtail-modeladmin-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!