User and Group Management
Explore how to create duplicate admin sites and register models with custom access. Learn to set user and group permissions, override permission methods, and filter model data based on business rules in Django Admin.
We'll cover the following...
Duplicating admin access
If you want to create different admins and provide them with access, you will have to form a new admin class
class MySecondAccessAdminSite(AdminSite):
site_header = "Second Admin site"
site_title = "Second Admin Portal"
index_title = "Welcome to your second dashboard"
second_admin_site = MySecondAccessAdminSite()
second_admin_site.register(Author, AuthorAdmin)
Then, you will need to add a new url to urls.py for your new admin (the second-admin in this example):
from sample_app.admin import site, second_admin_site
admin.site = site
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('second-admin/', second_admin_site.urls),
]
Do not forget to register the models you want to display in this new admin.
If you append /second-admin/ at the end of your web app URL instead of the /admin/ you will see your second admin page and it will show only the models you have registered with this new admin page.
Managing admin permissions for users and groups
You can create a new user by clicking on the add button next to the Users under the AUTHENTICATION AND AUTHORIZATION tab. Remember that you will need to register your User and Group models with the new admin site for AUTHENTICATION AND AUTHORIZATION in order for them to appear on the admin page:
second_admin_site.register(Group)
second_admin_site.register(User, UserAdmin)
To let ...