Wagtail ModelAdmin. Several sections for custom User

831 views Asked by At

I'm trying to set up Wagtail for existing custom User model. As per requirements several User pages should be available, representing different types of users (Regular, Manager, etc) I tried to make separate ModelAdmin for each case, overriding get_queryset for filtering by user type. But it looks like all of them show the first definition of ModelAdmin, as all of them has model - User

Then I tried to use Proxy model, in this case there is no display at all, as Wagtail seemingly doesn't support proxy models.

The only option I see now is to make my own views and add menu items leading to it

Please advice what is the best/easiest way to achieve this in Wagtail

3

There are 3 answers

1
Tantel Sacubitri On BEST ANSWER

Wagtail Admin actually works with Proxy Models. The missing part was that Wagtail permissions section doesn't include Proxy models, so you have to add it manually:

from wagtail.contrib.modeladmin.helpers import PermissionHelper

class ProxyModelPermissionHelper(PermissionHelper):
    def user_can_list(self, user):
        return True

and in ModelAdmin:

permission_helper_class = ProxyModelPermissionHelper
1
Kippster On

To get correct permissions when accessing models through SnippetViewSet, you can use this hook

from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from .models import MyModel

@hooks.register("register_permissions")
def register_ctf_permissions():
    model = MyModel
    content_type = ContentType.objects.get_for_model(model, for_concrete_model=False)
    return Permission.objects.filter(content_type=content_type)
0
Dan Swain On

Wagtail 2.5 now supports Django 2.2, and Django 2.2 now creates permissions for proxy models using the content type of the proxy model rather than the content type of the concrete model. Wagtail does not currently support setting proxy model permissions in the Wagtail admin, but if you update to Wagtail 2.5/Django 2.2, you can set group permissions for proxy models using the Django admin and you're good to go with no permission_helper_class needed.

I was assigning user rights for Groups and, contrary to the discussion in the below Github issue, was surprised to see that all proxy models appeared correctly after the Wagtail 2.5/Django 2.2 update (see this Github issue and the comment that this probably happened because the proxy models are registered in ModelAdmin). I have confirmed that the proxy models must be registered in ModelAdmin in order to appear in the groups permissions screen.

Don't forget to run migrate after updating to Django 2.2 as there is a migration that will migrate any existing proxy model permissions by switching the content type from the base model to the proxy model.