了解django admin readonly_fields [英] Understanding django admin readonly_fields

查看:353
本文介绍了了解django admin readonly_fields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些代码来区分Django管理员中的两个用户组,导致显示所有字段只读或只显示其中的一些,它们直接在ModelAdmin类中设置。



首先是代码:

  class PersonAdmin(admin.ModelAdmin):
readonly_fields =('created_at ','created_by',)

def get_form(self,request,obj = None,** kwargs):
如果obj:#我们正在编辑模式
如果请求.user.is_superuser:
self.readonly_fields =()
else:
request.user.groups.all()中的组:
如果str(组)==' readonlyuser':
allfields = tuple(obj._meta.get_all_field_names())
self.readonly_fields = allfields

返回超级(PersonAdmin,self).get_form(request,obj, ** kwargs)

我在组之间划分并相应地设置字段。如果两组用户没有同时登录,一切都可以正常运行!



我的检查提供了一个解决方案:
如果我为块中的管理员添加了一个额外的if语句,那么所有的功能都可以按预期运行。

  if str(group)=='adminuser':
self.readonly_fields = PersonAdmin.readonly_fields

为什么会发生什么?



我没有特殊的缓存设置,它发生在开发服务器以及Apache与WSGI上。 p>

从我的理解request.user.groups.all()应该返回用户所属的当前登录的 的所有组。 Django从哪里获取所有字段(只读),如果另一个IP和会话中的另一个用户如果阻塞,则匹配这个?

解决方案

ModelAdmin只对所收到的所有请求进行一次实例化。因此,当您定义这样的只读字段时,您将永久地将其设置。



只要运行Django 1.2+,就有一个 get_readonly_fields 方法,您可以直接使用此方法:

  class MyModelAdmin(admin .ModelAdmin):
...

def get_readonly_fields(self,request,obj = None):
如果request.user.is_superuser:
返回超级(MyModelAdmin ,self).get_readonly_fields(request,obj)
else:
return('created_at','created_by')

ModelAdmin 中删除​​ readonly_fields 属性,或将其设置为应该每个人都可以只读。然后,在 else 块中指定只对非超级用户只能读取的所有字段。


I created some code to differentiate between two usergroups in Django admin, resulting in showing all fields readonly or only some of them, which are set directly in the ModelAdmin class.

At first here is the code:

class PersonAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at','created_by',)

def get_form(self, request, obj=None, **kwargs):
    if obj:     # we are in edit mode
        if request.user.is_superuser:
            self.readonly_fields = ()
        else:
            for group in request.user.groups.all():
                if str(group) == 'readonlyuser':
                    allfields = tuple(obj._meta.get_all_field_names())
                    self.readonly_fields = allfields

    return super(PersonAdmin, self).get_form(request, obj, **kwargs)

I divide between the groups and set the fields accordingly. Everything works fine if users from the two groups are not logged in at the same time! After a 'readonly' user logged in, the adminuser will get all fields readonly too.

My inspections provided a solution also: If I put an additional if statement for the adminuser within the for block everything works as expected.

if str(group) == 'adminuser':
    self.readonly_fields = PersonAdmin.readonly_fields

Why is that and what's happening there?

I have no special cache settings made and it happens on the dev server as well as on an Apache with WSGI.

From my understanding request.user.groups.all() should return all groups the currently logged in user belongs to. Where does Django get the allfields (readonly) from, if another user on a different IP and session match this if block?

解决方案

The ModelAdmin is only instantiated once for all requests that it receives. So when you define the readonly fields like that, you're setting it across the board permanently.

As long as you're running Django 1.2+, there's a get_readonly_fields method you can use instead for exactly this purpose:

class MyModelAdmin(admin.ModelAdmin):
    ...

    def get_readonly_fields(self, request, obj=None):
        if request.user.is_superuser:
            return super(MyModelAdmin, self).get_readonly_fields(request, obj)
        else:
            return ('created_at', 'created_by')

Remove the readonly_fields attribute from your ModelAdmin or set it to the fields that should be readonly for everyone. Then, in the else block specify all the fields that should be readonly only for non-superusers.

这篇关于了解django admin readonly_fields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆