pythondjangowagtailwagtail-admin

How to add separate permission for a selected ModelAdmin in Django wagtail just like 'Page permissions'?


I am creating an application for teaching management in Wagtail. I create an AdminModal for 'Subjects'. I want to allow only selected user group to access a selected subject. Just like "Page permissions" in 'Add group'. Any idea how to do that?


Solution

  • You can do this by overriding get_queryset method in the ModelAdmin class that is associated with the Subject model.

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        valid_subjects = ['subject1', 'subject2', 'subject3']
        return qs.filter(subject_name__in=valid_subjects)
    

    The above example shows a way to restrict users from subject name. If you need to access group, you need to have a relation between Subject model and Group model. Then all you have to do is change the filter query.

    You can find more about querying in this documentation.