Django project initialization: extend the user table that comes with django, inherit the user table that comes with django

If you want to use django's built-in permission control function, you need to use django's own user table, but the fields in this table may not meet our business needs, and some fields may need to be expanded. It is necessary to inherit the user table that comes with django, and then expand the required fields.

Steps

1. Write the model class in models.py

from django.db import models
# Create your models here.
from django.contrib.auth.models import AbstractUser
#用户表
class User(AbstractUser):
    #继承原来的auth表,拓展字段,不要与原来有的字段名重复
    telephone = models.CharField(max_length=11,verbose_name='手机号码')
    icon = models.ImageField(upload_to='icon',default='/icon/default.jpg/',verbose_name='用户头像')
    name = models.CharField(max_length=12,verbose_name='用户姓名')
    sid = models.CharField(max_length=24,verbose_name='身份证')
    sex = models.IntegerField(choices=((1,'男'),(0,'女')),default=1,verbose_name='性别')
    role = models.IntegerField(choices=((1,'超级管理员'),(2,'管理员'),(3,'老师'),(4,'学生')),default=4,verbose_name='用户角色')
    entrance = models.CharField(max_length=10,verbose_name='入校年月')
    stuclass = models.ForeignKey(to='stuClass',null=True,on_delete=models.CASCADE,verbose_name='班级外键')

2. It is not enough after the model class is inherited, you need to tell django

In, configure in settings.py to tell django

AUTH_USER_MODEL = 'Application name. Model class name lowercase'

3. Do pay attention to the database migration command after performing the above operations

python manage.py makemigrations

python manage.py migrate

Guess you like

Origin blog.csdn.net/weixin_46371752/article/details/130456863