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!
The problem is that you're literally changing the
fieldsetsattribute on theModelAdmin, which is not thread-safe, even thoughget_fieldsetsis.The best way to do this is to specify separate fieldsets:
Then: