Exclude fields in Django admin for users other than superuser

后端 未结 6 1138
孤独总比滥情好
孤独总比滥情好 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:29

    I solved it this way inspired by previous answers. In my example only a superuser may create a superuser. If it is not superuser the checkbox in the form is missing. It works for me and I hope it is correct:

        def get_form(self, form_class=form_class):
    
            if self.request.user.is_superuser is False:
                self.form_class.base_fields.pop('is_superuser')
            return super(AccountCreateView, self).get_form()
    

提交回复
热议问题