Wagtail ModelAdmin read only
Asked Answered
Z

1

9

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
    [...]
Zacatecas answered 15/3, 2017 at 21:21 Comment(1)
Thank both of you for solution, it is only pity that inspect view break nice looking wagtail panels, it would be nice if there would be possibility to disable all fields, not break them to text. Anyway thank you.Adamis
R
9

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)
Rabideau answered 15/3, 2017 at 22:17 Comment(2)
Thanks! I also had to extend user_can_create, user_can_edit_obj, user_can_delete_obj for achieving the inspect view only. I've edited the question with the complete answer.Slant
Yes, I had initially included those methods in the answer but removed them so instead of forcing add/edit/delete to always be False, they can be set accordingly in the roles. The "drawback" to that is that the admins will have access regardless, but this allow to define multiple roles with different levels of control. But you're right, your question was about "unconditionally" make it read only and I should have included them.Haemagglutinate

© 2022 - 2024 — McMap. All rights reserved.