Matias Surdi escribió:
> I need to hide a couple fields from a model depending on the logged in 
> user in the admin interface.
> 
> How would you do this?
> 
> 
> > 
> 


Replying to myself, here is the solution:



class LimitedCustomUserAdmin(UserAdmin):
     inlines = [ProfileAdmin]
     form = forms.CustomUserChangeForm
     fieldsets = (
         (None, {'fields': ('username', 'password')}),
         ('Personal info', {'fields': ('first_name', 'last_name', 
'email')}),
         ('Permissions', {'fields': ('is_staff', 'is_active', 
'is_superuser')}),
         ('Important dates', {'fields': ('last_login', 'date_joined')}),
         ('Groups', {'fields': ('groups',)}),
     )



class CustomUserAdmin(LimitedCustomUserAdmin):
     fieldsets = (
         (None, {'fields': ('username', 'password')}),
         ('Personal info', {'fields': ('first_name', 'last_name', 
'email')}),
         ('Permissions', {'fields': ('is_staff', 'is_active', 
'is_superuser', 'user_permissions')}),
         ('Important dates', {'fields': ('last_login', 'date_joined')}),
         ('Groups', {'fields': ('groups',)}),
     )
     def __init__(self,*args,**kwargs):
         CustomUserAdmin.limited_user_admin = 
LimitedCustomUserAdmin(*args,**kwargs)
         return super(CustomUserAdmin,self).__init__(*args,**kwargs)

     def __call__(self, *args, **kwargs):
         if get_current_user().is_superuser:
             return super(CustomUserAdmin, self).__call__(*args, **kwargs)
         else:
             return CustomUserAdmin.limited_user_admin(*args,**kwargs)



This way, super_users can view/edit the fields defined in 
CustomUserAdmin and non super_users can view/edit the fields on 
LimitedCustomUserAdmin.


Note: You've to register only CustomUserAdmin with the User model.







--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to