Search code examples
pythondjangowagtailmodeladmin

Wagtail ModelAdmin read only


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

Solution

  • 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)