Django ModelAdmin with request/user based restrictions on the fieldsets (needs to be Thread Safe)

拟墨画扇 提交于 2020-01-25 06:28:04

问题


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

def get_fieldsets(self, request, obj=None):
    fieldsets = super(PageAdmin, self).get_fieldsets(request, obj)
    if request.user.is_superuser:
        fieldsets[0][1]['fields'].insert(0,'group')
        fieldsets[0][1]['fields'].insert(2,'is_live')
    else:
        groups = request.user.page_groups.filter(
            is_live = True,
        )
        if groups.count() > 1:
            fieldsets[0][1]['fields'].insert(0,'group')
    return fieldsets

This works (sort of) and I like using get_fieldsets because it lets me group the fields into fieldsets. I also use get_form on this admin because the form has several user specific form fields that have querysets based on the user.

def get_form(self, request, obj=None, **kwargs):
    if request.user.is_superuser:
        return PageForm
    else:
        form = RestrictedPageForm
        form.owner = request.user #(this may be a bad way to do this but it works I think)
        return form

Now I am running into what I believe to be Threading issues.

What happens is that if you quickly refresh the change_form page in the browser you will see multiple "group" or "is_live" fields in the form.

I have really liked leveraging the Admin to keep me from having to write everything but I cannot find out how to do this correctly. Any help or suggestions would be greatly appreciated!


回答1:


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)


来源:https://stackoverflow.com/questions/11764685/django-modeladmin-with-request-user-based-restrictions-on-the-fieldsets-needs-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!