https://docs.djangoproject.com/zh-hans/3.2/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
用model.py来补充User模型的信息class EmployeeUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(blank=True, null=True,choices=DEPT_TYPE, max_length=100, verbose_name=u'部门')
title = models.CharField(blank=True, null=True,max_length=100, verbose_name=u'职务')
role = models.CharField(blank=True, null=True,max_length=100, choices=ROLE_TYPE,verbose_name=u'角色')
admin.py登记User模型
from django.contrib import admin
from training.models import *
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
# 拓展User模型
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = EmployeeUser
can_delete = False
verbose_name_plural = 'EmployeeUser'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline,)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
views.py使用User模型的拓展
@login_required
def deptReviewlist(request):
'''部门主管查看建议的list页面'''
# 查看用户权限
u = User.objects.get(username=request.user)
user_department = u.employeeuser.department
user_role= u.employeeuser.role
print(user_role)
# 如果是超级管理员,就全部查看;否则就按部门查看和role
if u.is_superuser:
dept_review_suggestion_self = dept_review.objects.all()
elif user_role == '初审人员':
dept_review_suggestion_self = dept_review.objects.all()
else:
# 只有复审人员才能去查看并审核表单
if user_role == '复审人员':
dept_review_suggestion_self = dept_review.objects.filter(dept_review_suggestion_dept_reviewer=user_department)
else:
dept_review_suggestion_self = None
# 上下文内容字典
context = {
'object_list': dept_review_suggestion_self,
'user_role': user_role,
}
return render(request, 'dept_review_list.html',context=context)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)