实现这一目标的方法之一是创建 2 个管理站点,一个供管理员使用,另一个供员工使用。考虑下面的 HR 和 Root 管理员示例:
- 在
admin.py
from django.contrib import admin
from django.contrib.admin import AdminSite
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import get_user_model
from core.models import CV
User = get_user_model()
def has_superuser_permission(request):
return request.user.is_active and request.user.is_superuser
# Only superuser can access root admin site (default)
admin.site.has_permission = has_superuser_permission
class HRAdminSite(AdminSite):
"""HR admin page definition"""
site_header = "HR Admin"
hr_admin_site = HRAdminSite(name='hr_admin')
# available only to super_users
@admin.register(User)
class RootUserAdmin(UserAdmin):
pass
# available to both types of admins hr (is_staff) and root (is_superuser)
@admin.register(CV)
@admin.register(CV, site=hr_admin_site)
class CVAdmin(admin.ModelAdmin):
pass
- 在
urls.py
from django.contrib import admin
from core.admin import hr_admin_site
urlpatterns = [
path("root/", admin.site.urls),
path("hr/", hr_admin_site.urls),
]