Field Level Permission Django

djangobot picture djangobot · Dec 5, 2013 · Viewed 7.4k times · Source

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):
    field1 = .....
    field2 = .....
    field3 = .....

Create two groups(A and B) and assigned permission that both one can add/edit/delete and the other can only add/edit. But now need some help in this :-

I want if the first group user logs in in the admin he should be able to see all the three fields but if second group user logs in they should only see field1.

I want this in django admin as i need to perform some manipulations after these.My django version is 1.3

Thanks in advance

Answer

FallenAngel picture FallenAngel · Dec 5, 2013

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.