Inherit the users table and add new fields to a new table

1. Tools > Run manage.py Task to create app, users

startapp users

2. Modify the models in users

from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.

class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50, verbose_name='昵称', default='')
    birthday = models.DateField(verbose_name='生日', null=True, blank=True)
    gender = models.CharField(max_length=10, choices=(('male', ''), ('female', '')), verbose_name='性别',
                              default='female')
    address = models.CharField(max_length=100, default='')
    mobile = models.CharField(max_length=11, verbose_name= ' mobile phone number ' , null=True, blank= True)
    image = models.ImageField(max_length=100, verbose_name='用户头像', upload_to='static/uploads/images/%Y/%m',
                              default='static/uploads/images/default.png')

    class Meta:
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username

3. Modify the configuration file settings

 

 

AUTH_USER_MODEL = 'users.UserProfile' should change the default user table from auth_users to the custom users.userprofile 

4. Data migration
makemigrations users 会出错,django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.

Use the online map, so you can only re-migrate all the previous data, and it will be solved.

direct:

makemigrations
migrate

 

 Seems like auth_users is gone

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325195296&siteId=291194637