updating the readonly_fields in django based on the change permission


i have python django project where i am using to display all the books and rendering
a single book when clicking on each book so django will automatically generate the
book details.

so while rendering i want to make the price_upgrade boolean field read only if the user doesn’t have a
group called author

so my codes like this

class BookAdmin(admin.ModelAdmin):
    list_per_page = 10
    list_display_links = ['name']
    readonly_fields = ('id', 'name', 'city', 'price_upgrade')

def has_change_permission(self, request, obj=None):
        if request.user.groups.all().filter(Group ='author').exists():
            print('author group is there')
            print('author group is not there')
        #print('request', request.user.groups.all(), type(request.user.groups.all()))
        return True

how can i add the price_upgrade field to readonly_fields if current user doesn’t have the group of
author else we should remove the price_upgrade field from readonly_fields because he is part of
author group so he can edit it

python 2.7
django 1.8

Any help appreciated

Asked By: Learner



You can override the changeform_view like this in your BookAdmin

       def changeform_view(self, request, *args, **kwargs):
            self.readonly_fields = list(self.readonly_fields)
            usergroup = request.user.groups.filter(name__in=['author']).exists()
            if not usergroup:

            return super(BookAdmin, self).changeform_view(request, *args, **kwargs)

you can override the get_readonly_fields method in your Admin

def get_readonly_fields(self, request, obj=None):
    usergroup = request.user.groups.filter(name__in=['author']).exists()
    if not usergroup:
       # since readonly_fields is a tuple we need to append to tuple
        self.readonly_fields = self.readonly_fields + ('price_upgrade',)
    return self.readonly_fields

The issue why it was overriding the value based on the comment is because it was running on the same port (same instance), so made the project up and running in two different port so the issue was not there. Since when your are accessing the same port it will have the same permission for the both users so we need to restart the server.

This was what i found, kindly update if any miss info is there

There is a caching issue is there thats why its overriding the permission, need to find a solution for it, the problem is if you logged in with a different user doesn’t have the author group and logout and login with a user has author group will not be able to edit the checkbox, so there is an issue of caching
Will update it soon once i found out a solution.
If i restart the server it works fine, but restarting the server is not the proper solution.

Related ref

So after going through the issue, found out the solution

def get_readonly_fields(self, request, obj=None):
        readOnlyFields = super(BookAdmin, self).get_readonly_fields(request, obj)
        usergroup = request.user.groups.filter(name__icontains='author').exists()
        if not usergroup:
            readOnlyFields = readOnlyFields + ('price_upgrade',)
        return readOnlyFields
Answered By: arjun

In the method described above, I had a problem when there were requests from a user who had permissions (in my case custom) and another user who did not have permissions. Hence my solution which dynamically refreshes and checks without the need to restart the server.

Instead of entering sections in the admin model that will always be read-only, we put them in the second line instead of "[‘dynamic_average_rating’]".

    def changeform_view(self, request, *args, **kwargs):
    self.readonly_fields = ['dynamic_average_rating']
    has_perm_edit_slug = request.user.has_perm('places.can_edit_slug')
    if not has_perm_edit_slug:

    self.readonly_fields = tuple(self.readonly_fields)

    return super(PlaceAdmin, self).changeform_view(request, *args, **kwargs)
Answered By: JP-Ekuzen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.