django重写user模型

时间:2022-10-18 11:56:41

django重写user模型

model.py定义
from django.contrib.auth.models import AbstractUser # 引入user模型的基础类
class User(AbstractUser):
cn_name = models.CharField(u'中文名', max_length=30, blank=True)
department = models.ForeignKey(Department, related_name="department_user", verbose_name='所属部门',
on_delete=models.PROTECT)
dingtalk_id = models.CharField('钉钉ID', max_length=30)
mobile = models.CharField('电话', null=True,blank=True,max_length=15)
upuid = models.ForeignKey('self',verbose_name="上级主管", null=True,blank=True,on_delete=models.SET_NULL)

class Meta(AbstractUser.Meta):
swappable = 'AUTH_USER_MODEL'
setting.py定义 django默认使用这个User类
AUTH_USER_MODEL = "asset.User"  # asset是应用名
获取user模型
from django.contrib.auth import get_user_model
user = get_user_model()