Exclude field in django admin for non super admin users

How do I exclude a field in django admin if users are not super admin?

thanks

+6
django django-admin django-forms
source share
4 answers

I did it as follows:

admin.py

def add_view(self, request, form_url='', extra_context=None): if not request.user.is_superuser: self.exclude=('activa', ) return super(NoticiaAdmin, self).add_view(request, form_url='', extra_context=None) 
+4
source share

Overriding the exclude property is a bit dangerous if you remember to set it for other permissions, the best way would be to override the get_form method.

see: Django admin: exclude field only in change form

+3
source share

In the future, there seems to be a get_fields hook. But this is only in the main branch, and not in 1.5 or 1.6.

 def get_fields(self, request, obj=None): """ Hook for specifying fields. """ return self.fields 

https://github.com/django/django/blob/master/django/contrib/admin/options.py

+1
source share

If you have many views, you can use this decorator:

 def exclude(fields=(), permission=None): """ Exclude fields in django admin with decorator """ def _dec(func): def view(self, request, *args, **kwargs): if not request.user.has_perm(permission): self.exclude=fields return func(self, request, *args, **kwargs) return view return _dec 

usage: @exclude (fields = ('fonction', 'fonction_ar'))

0
source share

All Articles