Django Builds online education platform _day_2: New users app to write models extension user table

Source: Internet
Author: User

1. Create an app using Pycharm

Tools toolbar running Run manage.py Task

Input: Startapp Users

2. To see the Default Users table Auth_User that Django generates, and to see if the fields meet the needs, you cannot write models custom user table Inheritance Auth_User fields in users/models.py.

 fromDjango.dbImportModels#Abstractuser contains the default generated user field for Django fromDjango.contrib.auth.modelsImportAbstractuser#Create your models here.classuserprofile (abstractuser):"""inherit the Abstractuser class, add a database field from a new class of meanings"""    #default= ' default is emptyNick_name = models. Charfield (max_length=50, Verbose_name=u'Nickname', default="')    #null=true, Blank=true allows users not to fill inBirday = models. Datefield (verbose_name=u'Birthday', Null=true, blank=True)#Choices Property, which provides the selected data, where sex is only male and femaleGender = models. Charfield (max_length=5, choices= ('male','male'), ('female','female')), default='female', Verbose_name=u'Sex')#SexAddress = models. Charfield (max_length=100, Default=u"', Verbose_name=u'Address') Mobile= Models. Charfield (max_length=11, Null=true, Blank=true, Verbose_name=u'Phone number')    #upload_to Specify Avatar Upload folderImage = Models. ImageField (upload_to='image/%y/%m', Default=u'Image/default.png', Verbose_name=u'Avatar', max_length=100)    classMeta:verbose_name= R'User Information' #别称verbose_name_plural=verbose_name #单数别称def __unicode__(self):"""overloaded functions to enable custom strings to be printed"""        returnSelf.username

3. Registering apps and overloaded methods in mxonline/settings.py

#application DefinitionInstalled_apps= [    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Users',#Register App]#overloaded Auth_user_model method, app name + Custom models class nameAuth_user_model ='users. UserProfile'

4, the operation of the project, the following error occurred, the reason is that we use the ImageField in the custom class, need to install the Pillow image processing module:

Installing Pillow:pip install pillow, rerun, report the following error because the database does not have the users table

To create the Users table:

Tools toolbar running Run manage.py Task

Input: Makemigrations Users

Input: Migrate Users

Input: Yes

View database table input Users_userprofile created successfully

Run the project again, success

Django Builds online education platform _day_2: New users app to write models extension user table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.