1.在apps/user/model.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
mobile = models.CharField(verbose_name='手机号', unique=True, max_length=11)
email_active = models.BooleanField(verbose_name='邮箱激活状态', default=False)
class Meta:
db_table = 'User'
verbose_name = '用户'
verbose_name_plural = verbose_name
2.在settings.py中添加配置项
AUTH_USER_MODEL = 'user.User'
3.执行迁移文件
python manage.py makemigrations
python manage.py migrate
在执行迁移文件中报错
django.db.migrations.exceptions.InconsistentMigrationHistory:
Migration admin.0001_initial is applied before its dependency
user.0001_initial on database ‘default’.
产生的原因:
在自定义django认证系统的用户模型类之前,已经执行了迁移文件
,会与现有自定义模型类冲突
解决办法:
先makemigrations后再注释掉INSTALL_APPS中的admin和urlpatterns中的admin,此时再migrate就不会报错,记得注释改回来
tips:建议项目初期直接自定义模型类,否则后期维护会很麻烦!!!