Exclude fields in Django admin for users other than superuser

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

    If I understand you correctly, what you want to do is override the get_form method for the ModelAdmin. Base on the example from django documentation, it would look something like this:

    class MyUserAdmin(admin.ModelAdmin):
        def get_form(self, request, obj=None, **kwargs):
            self.exclude = []
            if not request.user.is_superuser:
                self.exclude.append('Permissions') #here!
            return super(MyUserAdmin, self).get_form(request, obj, **kwargs)
    

    Now you might need to hack around a little and maybe override the save method as well. I did something similar not long ago, it's not so complicated (and the docs are fantastic).

    There might be a simpler solution but your question is kinda general and you didn't share your user model, so I can't tell you exactly how to work this out. I hope this helps!

提交回复
热议问题