1. 继承AbstractUser:
在你对应的your_app/models.py文件添加
from django.contrib.auth.models import AbstractUser
#自定义你想要的字段
class User(AbstractUser):
faculty = models.ForeignKey(Faculty, on_delete=models.SET_NULL, default=None, null=True)
major = models.ForeignKey(Major, on_delete=models.SET_NULL, default=None, null=True)
classes = models.ForeignKey(Classes, on_delete=models.SET_NULL, default=None, null=True)
team = models.ForeignKey(Team, on_delete=models.SET_NULL, default=None, null=True)
student_id = models.CharField(max_length=100, null=True, default=None)
logo = models.ImageField(upload_to=‘user_logo‘, null=True, default=None)
nickname = models.CharField(max_length=100, null=True)
mobile = models.CharField(max_length=16, null=True, default=None)
address = models.CharField(max_length=80, null=True, default=None)
ID_number = models.CharField(max_length=40, null=True, default=None)
brief_introduction = models.TextField(null=True)
#邮箱验证
email_validate = models.BooleanField(default=False)
USER = enum(
DELETE=0,
NORMAL=1,
NEW_REGISTER=2,
PASS=3,
)
status = models.PositiveIntegerField(default=USER.NORMAL)
2. 添加setting配置:
在项目主setting,py文件中添加如下
AUTH_USER_MODEL=‘your_app.User‘
#其中your_app 为你自定义的app,必须已经存在INSTALLED_APPS中,User为你的类名
3. 执行数据表同步命令:
python manage.pmakemigrations
python manage.pmigrate # 执行的时候需要yes同意
# 当你已经创建过超级用户的时候,需要重新创建,因为user已经被改了