tags:

views:

9

answers:

1

I have a Model that's managed by Django Admin. How to I customize the display of its edit form based on the currently logged in user? For example if someone other than the superuser is modifying the model, I want to hide certain fields.

It'd be great if I could set fields based on request.user

+1  A: 

A hackish way to do this is overwriting the list_display variable EVERY time the changelist view is called:

class MyModelAdmin(admin.ModelAdmin)
    def changelist_view(self, request, extra_context=None):
        user = request.user
        if user.is_superuser:
            self.list_display = ['field1', 'field2']
        else:
            self.list_display = ['field1']
        return super(MyModelAdmin, self).changelist_view(request, extra_context=None)

But set the variable every time to the desired value, since the same admin instance might be called for requests made by other users as well!

lazerscience
sorry made a mistake in my question, I really meant `fields` instead of `list_display`, but your suggestion should still apply
zer0stimulus