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?
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!
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With