Reputation: 6865
how i'll exclude a field in django admin if the users are not super admin?
thanks
Upvotes: 5
Views: 3812
Reputation: 11
In the future, it looks like there will be a get_fields hook. But it's only in the master branch, not 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
Upvotes: 1
Reputation: 1534
Overriding the exclude property is a little dangerous unless you remember to set it back for other permissions, a better way might be to override the get_form
method.
see: Django admin: exclude field on change form only
Upvotes: 3
Reputation: 1252
If you have a lot of 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'))
Upvotes: 0
Reputation: 6865
I did it in this way:
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)
Upvotes: 4