此方法建立在新建项目的基础上,不适合更改原有的项目。
重写user的app中model.py文件内容如下:
models.py
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
"""
用户
"""
name = models.CharField(max_length=30, null=True, blank=True, verbose_name="姓名")
birthday = models.DateField(null=True, blank=True, verbose_name="出生年月")
gender = models.CharField(max_length=6, choices=(("male", u"男"), ("female", "女")), default="female", verbose_name="性别")
mobile = models.CharField(null=True, blank=True, max_length=11, verbose_name="电话")
email = models.EmailField(max_length=100, null=True, blank=True, verbose_name="邮箱")
class Meta:
verbose_name = "用户"
verbose_name_plural = verbose_name
def __str__(self):
return self.username
setting.py中需要加入如下内容:
AUTH_USER_MODEL = 'MyUsers.UserProfile' #MyUsers为app的名称 ,UserProfile为刚才上方定义的model
如此以来执行以下命令就能替换原有的user
python manage.py makemigrations
python manage.py migrate
网友评论