Django restframework扩展自带的User属性
Django框架认证自带了User系统,但是有些常用的字段并没有,比如mobile,wxid这些比较有中国市场特点的。
查看源码可以发现其是基于AbstractUser的,所以我们自定义User并继续该抽象类,接着扩展自己的字段即可
class User(AbstractUser):
name = models.CharField(max_length=14,null=True)
uuid = models.UUIDField(editable=False,unique=True,null=False)
wxId = models.CharField(max_length=50, null=True)
alipayId = models.CharField(max_length=50, null=True)
appleId = models.CharField(max_length=50, null=True)
phone = models.CharField(max_length=20, null=True)
继承写完,接下来要做一番配置
在settings.py中添加AUTH_USER_MODEL = 'HelloWorld.User',注意中间并没有models
最后,执行数据迁移即可
python3 manage.py makemigrations
python3 manage.py migrate
版权属于:邢迪的平行时空
本文链接:https://xingdi.me/archives/83.html
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可