修改django默认的user表

1. 创建users的app,并在model.py中写

注:一定要将email设置成外键,因为一个邮箱只能对应一个帐号,这样才能进行密码找回

class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50,verbose_name=u'昵称',default=u"")
    birday = models.DateField(verbose_name=u"生日",null=True,blank=True)
    gender = models.CharField(max_length=6,choices=(('male',u'男'),('female',u'女')),default='female')
    address = models.CharField(max_length=100,default=u"")
    mobile = models.CharField(max_length=11,verbose_name=u"手机",null=True,blank=True)
    image = models.ImageField(upload_to="image/%Y/%m",default="image/2018/05/default_middile_2.png",max_length=100)

    def __str(self):
        return self.username

    class Meta:
        verbose_name = u"用户信息"
        verbose_name_plural = verbose_name
        unique_together = (("email"),)

2. 然后在settings中注册app

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
]
AUTH_USER_MODEL  = "users.UserProfile"

3. 运行 python manage.py migrate  输入 yes 

(testvir2) E:\myGitHup\python\DjangoDdu>python manage.py migrate
Operations to perform:
  Apply all migrations: sessions, auth, users, admin, contenttypes, t1
Running migrations:
  Rendering model states... DONE
  Applying users.0001_initial... OK
The following content types are stale and need to be deleted:


    auth | user

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.


    Type 'yes' to continue, or 'no' to cancel: yes

猜你喜欢

转载自blog.csdn.net/qq_34964399/article/details/80290236
今日推荐