本文介绍了Django ModelAdmin与请求/基于用户的对fieldsets的限制(需要为Thread Safe)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个非常自定义的Django ModelAdmin,如果用户是超级用户,我想添加额外的字段。我发现有人说要覆盖像这样的get_fieldsets方法

I have several very customized Django ModelAdmins that I would like to add extra field(s) if the user is a superuser. I found somewhere that someone said to override the get_fieldsets method like this

 c  ModelAdmin 中的fieldets 属性不是线程安全的em>是。

The problem is that you're literally changing the fieldsets attribute on the ModelAdmin, which is not thread-safe, even though get_fieldsets is.

最好的方法是指定单独的字段集:

The best way to do this is to specify separate fieldsets:

fieldsets = (...)
restricted_fieldsets = (...)

然后:

Then:

def get_fieldsets(self, request, obj=None):
    if some_condition:
        return self.restricted_fieldsets
    else:
        return super(MyModelAdmin, self).get_fieldsets(request, obj=obj)

这篇关于Django ModelAdmin与请求/基于用户的对fieldsets的限制(需要为Thread Safe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 21:13