How to limit fields in django-admin depending on user?

后端 未结 4 1432
無奈伤痛
無奈伤痛 2020-12-24 12:56

I suppose similar problem would have been discussed here, but I couldn\'t find it.

Let\'s suppose I have an Editor and a Supervisor. I want the Editor to be able to

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 13:15

    Starting Django 1.7, you can now use the get_fields hook which makes it so much simpler to implement conditional fields.

    class MyModelAdmin(admin.ModelAdmin):
        ...
    
        def get_fields(self, request, obj=None):
            fields = super(MyModelAdmin, self).get_fields(request, obj)
            if request.user.is_superuser:
                fields += ('approve',)
    
            return fields
    

提交回复
热议问题