Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 633
Asinox Avatar asked Aug 12 '10 04:08

Asinox


People also ask

How do I restrict access to parts of Django admin?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .

How do you make a field non mandatory in Django admin?

The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank).

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Literally saved my life!

What is Fieldsets in Django admin?

fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a “section” of the form.)


3 Answers

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)
like image 127
Asinox Avatar answered Oct 16 '22 12:10

Asinox


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

like image 42
Steve Pike Avatar answered Oct 16 '22 11:10

Steve Pike


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

like image 44
Keizo Gates Avatar answered Oct 16 '22 11:10

Keizo Gates