简体   繁体   English

根据更改权限更新 django 中的 readonly_fields

[英]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.我有 python django 项目,我用于显示所有书籍并在单击每本书时呈现一本书,因此 django 将自动生成书籍详细信息。

so while rendering i want to make the price_upgrade boolean field read only if the user doesn't have a group called author因此,在渲染时,我想让 price_upgrade boolean 字段只读,前提是用户没有名为 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如果当前用户没有作者组,我如何将 price_upgrade 字段添加到 readonly_fields 否则我们应该从 readonly_fields 中删除 price_upgrade 字段,因为他是作者组的一部分,因此他可以编辑它

Version python 2.7 django 1.8版本 python 2.7 django 1.8

Any help appreciated任何帮助表示赞赏

You can override the changeform_view like this in your BookAdmin您可以在 BookAdmin 中像这样覆盖 changeform_view

       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另一个选项:更新后您可以覆盖管理员中的 get_readonly_fields 方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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