我想在管理员后端只向超级用户显示模型,但不向员工用户显示,是否可以?
示例:
check which user is logged
if superuser
admin.site.register(model_1)
admin.site.register(model_2)
admin.site.register(model_3)
...
if staff_user
admin.site.register(model_1) #show only one model in the admin
谢谢
答案 0 :(得分:3)
它无法正常工作。您的admin.py
模块在进程启动时被导入并被唤醒,而不是每次进程处理请求时(希望其他情况会非常缓慢)。解决方案是使用django的权限系统:https://docs.djangoproject.com/en/1.5/topics/auth/default/#permissions-and-authorization
答案 1 :(得分:1)
实现这一目标的方法之一是创建 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),
]
答案 2 :(得分:0)
创建组并将其分配给您的员工用户并为该组分配权限。只有您为该组分配的模块和权限才能查看其他人员用户。