Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django:Remove superuser checkbox from Django admin panel when login staff users

I need to edit the django admin panel. In my application, superuser can add users and can assign the privileges. when super user add staff user like HR manager, application should allow to add users. it works. but I need do is when staff user log to the admin panel hide the superuser status bar. how can I do this? what Django admin file should I change? and How?

like image 502
Lahiruzz Avatar asked Apr 19 '13 10:04

Lahiruzz


People also ask

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!

Can we customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

Can I delete superuser in Django?

There is no way to delete it from the Terminal (unfortunately), but you can delete it directly. Just log into the admin page, click on the user you want to delete, scroll down to the bottom and press delete.

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 .


1 Answers

If you want your superuser to still be able to add other superusers through the admin, but not allow staff users to manage superusers, you'll need to create a custom admin that overrides the get_fieldsets method. This can go in your admin.py file:

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.utils.translation import ugettext, ugettext_lazy as _

class MyUserAdmin(UserAdmin):
    def get_fieldsets(self, request, obj=None):
        if not obj:
            return self.add_fieldsets

        if request.user.is_superuser:
            perm_fields = ('is_active', 'is_staff', 'is_superuser',
                           'groups', 'user_permissions')
        else:
            # modify these to suit the fields you want your
            # staff user to be able to edit
            perm_fields = ('is_active', 'is_staff')

        return [(None, {'fields': ('username', 'password')}),
                (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
                (_('Permissions'), {'fields': perm_fields}),
                (_('Important dates'), {'fields': ('last_login', 'date_joined')})]

Follow the procedures to unregister the django User admin and register your new User admin:

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

You can do similar things with any model where you want a different set of fieldsets for one set of users versus another -- just override the get_fieldsets method in your custom admin and use the request object to determine which user is trying to access it.

like image 75
Jay Avatar answered Oct 25 '22 14:10

Jay