1

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!

byoungb
  • 1,771
  • 20
  • 31

1 Answers1

4

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)
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for getting me on the right track, I didn't realize that I was actually updating the actual fieldsets attribute of the ModelAdmin. Now that seems pretty obvious. – byoungb Aug 02 '12 at 01:04
  • Oh and for anyone like me who has a large complicated fieldsets list and just wants to add a field or two conditionally I used deepcopy in the get_fieldsets so that I could avoid having to maintain two or more complicated fieldsets lists. – byoungb Aug 02 '12 at 01:07