I’ve tried various methods to achieve this.
I decided against overriding formfield_for_dbfield as it doesn’t get a copy of the request object and I was hoping to avoid the thread_locals hack.
I settled on overriding get_form in my ModelAdmin class and tried the following:
class PageOptions(admin.ModelAdmin): def get_form(self, request, obj=None, **kwargs): if request.user.is_superuser: self.fieldsets = ((None, {'fields': ('title','name',),}),) else: self.fieldsets = ((None, {'fields': ('title',),}),) return super(PageOptions,self).get_form(request, obj=None, **kwargs)
When I print fieldsets or declared_fieldsets from within get_form I get None (or whatever I set as an initial value in PageOptions).
Why doesn’t this work and is there a better way to do this?
I have no idea why printing the property doesn’t give you want you just assigned (I guess may be that depends on where you print, exactly), but try overriding
get_fieldsetsinstead. The base implementation looks like this:I.e. you should be able to just return your tuples.
EDIT by andybak. 4 years on and I found my own question again when trying to do something similar on another project. This time I went with this approach although modified slightly to avoid having to repeat fieldsets definition: