简体   繁体   中英

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')
        else:
            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

Version python 2.7 django 1.8

Any help appreciated

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:
                self.readonly_fields.append('price_upgrade')

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

AOTHER OPTION: Updated 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

@ Update 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM