Field Level Permission Django

前端 未结 3 2011
一整个雨季
一整个雨季 2021-02-05 20:27

Today i came up with a requirement where i need to implement field level permission so looking for the best possible way.

class ABC(models.Model):
    fiel         


        
3条回答
  •  后悔当初
    2021-02-05 21:15

    In your admin.py

    class ABCAdmin(admin.ModelAdmin):
        fields = [.....]  # here comes the fields open to all users
    
        def change_view(self, request, object_id, extra_context=None):  # override default admin change behaviour
            if request.user in gruop2:  # an example 
                self.fields.append('field2')  # add field 2 to your `fields` 
                self.fields.append('field3')  # add field 3 to your `fields`
    

    You can use the docs to see what is available. Above is an example taken from one of my usages. You may also need to define change_view and add_view too.

提交回复
热议问题