Extending users table in Django app

Time:2020-10-11
  • Write a new user in app models

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 ා inherits user
  6
  7 # Create your models here.
  8
  9
 10 class  UserProfile(AbstractUser):
 11     nick_ name =  models.CharField (max_ length=50, verbose_ Name = u 'nickname', default=
    '')
 12     birday =  models.DateField (verbose_ Name = u 'birthday', null = true, blank = true)
 13     gender =  models.CharField (choices = ('male ', u'male'), ('female ', u'female')), 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: 'user information'
 20         verbose_name_plural = verbose_name
 21
 22     def __unicode__(self):
 23         return self.username
  • Modify the settings.py file

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 'ා specifies a new users model
  • Migration table

python manage.py makemigrations users
python  manage.py  Migrate users ා when running this statement, you can choose whether to delete the users table existing in the original database, and select Delete