Exclude fields in Django admin for users other than superuser

后端 未结 6 1137
孤独总比滥情好
孤独总比滥情好 2021-02-04 04:34

I have a simple MyUser class with PermissionsMixin. user.is_superuser equals True only for superusers. I\'d like to be able t

6条回答
  •  粉色の甜心
    2021-02-04 05:10

    According to Django Docs, the correct way is to create a ModelForm for superusers and another for normal users. Then you specify each form in the get_form method of your ModelAdmin:

    class MyModelAdmin(admin.ModelAdmin):
        def get_form(self, request, obj=None, **kwargs):
            if request.user.is_superuser:
                kwargs['form'] = MySuperuserForm
            else:
                kwargs['form'] = MyNormalForm
            return super(MyModelAdmin, self).get_form(request, obj, **kwargs)
    

提交回复
热议问题