1. 程式人生 > >django擴展User表

django擴展User表

code classes models markdown stat keyword 郵箱 register enum

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已經被改了

django擴展User表