1

django重写user模型

 1 year ago
source link: https://blog.51cto.com/linyingyong/5765437
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

django重写user模型

精选 原创

lyy910203 2022-10-18 10:26:37 博主文章分类:python ©著作权

文章标签 django 基础类 文章分类 Python 编程语言 阅读数147

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()
  • 打赏
  • 收藏
  • 1评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK