6

how i'll exclude a field in django admin if the users are not super admin?

thanks

Asinox
  • 6,747
  • 14
  • 61
  • 89

4 Answers4

4

I did it in this way:

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)
Community
  • 1
  • 1
Asinox
  • 6,747
  • 14
  • 61
  • 89
3

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

Community
  • 1
  • 1
Steve Pike
  • 1,454
  • 2
  • 12
  • 14
1

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

Keizo Gates
  • 11
  • 1
  • 1
0

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'))

surfeurX
  • 1,262
  • 12
  • 16