Asinox
Asinox

Reputation: 6865

Exclude a field in django admin for users not super admin

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

thanks

Upvotes: 5

Views: 3812

Answers (4)

Keizo Gates
Keizo Gates

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

Steve Pike
Steve Pike

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

surfeurX
surfeurX

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

Asinox
Asinox

Reputation: 6865

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)

Upvotes: 4

Related Questions