django app中扩展users表

  • app models中编写新的User

 1 # _*_ coding:utf-8 _*_
  2 from __future__ import unicode_literals
  3
  4 from django.db import models
  5 from django.contrib.auth.models import AbstractUser   # 继承user
  6
  7 # Create your models here.
  8
  9
 10 class  UserProfile(AbstractUser):
 11     nick_name = models.CharField(max_length=50, verbose_name=u'昵称', default=
    '')
 12     birday = models.DateField(verbose_name=u'生日', null=True, blank=True)
 13     gender = models.CharField(choices=(('male', u'男'), ('female', u'女')), de
    fault='female', max_length=10)
 14     address = models.CharField(max_length=100, default=u'')
 15     mobile = models.CharField(max_length=11, null=True, blank=True)
 16     image = models.ImageField(upload_to='image/%Y/%m', default=u'image/default
    .png', max_length=100)
 17
 18     class Meta:
 19         verbose_name = '用户信息'
 20         verbose_name_plural = verbose_name
 21
 22     def __unicode__(self):
 23         return self.username
  • 修改项目的settings.py 文件

 36 INSTALLED_APPS = [
 37     'django.contrib.admin',
 38     'django.contrib.auth',
 39     'django.contrib.contenttypes',
 40     'django.contrib.sessions',
 41     'django.contrib.messages',
 42     'django.contrib.staticfiles',
 43
 44     'users',
 45 ]
 46
 47 AUTH_USER_MODEL = 'users.UserProfile'  # 指定新的users model
  • 迁移表

python manage.py makemigrations users
python manage.py migrate users   # 在运行这条语句的时候,会让你选择是否删除原来数据库里面存在的users表,选择删除即可
    原文作者:Rhinoceros
    原文地址: https://segmentfault.com/a/1190000009296151
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞