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
class EmployeeInline(admin.StackedInline):
model = EmployeeUser
can_delete = False
verbose_name_plural = 'EmployeeUser'
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline,)
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)
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)
|