Django admin - change permissions list

后端 未结 2 1857
有刺的猬
有刺的猬 2021-02-03 13:15

Is there any possibility to change permissions list in user edit page? I don\'t wan\'t to show all of permissions for example admin log entry or auth group etc. How can I modify

2条回答
  •  没有蜡笔的小新
    2021-02-03 13:28

    Renato's answer is almost perfect. The Django Admin makes adding a user a two-step process with the same form, and his code fails with a KeyError for 'user_permissions' in the first step.

    The fix is easy enough, just use the code below instead:

    def get_form(self, request, obj=None, **kwargs):
        form = super(MyUserAdmin, self).get_form(request, obj, **kwargs)
        # adding a User via the Admin doesn't include the permissions at first
        if 'user_permissions' in form.base_fields:
            permissions = form.base_fields['user_permissions']
            permissions.queryset = permissions.queryset.filter(content_type__name='log entry')
        return form
    

提交回复
热议问题