Python3 Django connection MySQL, synchronization table structure

Source: Internet
Author: User

First step: Install PYMYSQ instead of MySQLdbPIP3 Install Pymysqlthen fill in the following two sentences in the __init__.py of the engineering catalogue Import Pymysql
pymysql.install_as_mysqldb ()   issue: No module named ' MySQLdb ' if prompted during installation Workaround: Python3 Use the above method instead of MySQLdbcause: In Python3, the MYSQLDB package is no longer usedStep Two: Configure the database and Installed_apps (settings.py under App) Configuration DatabaseDATABASES = {
' default ': {
' ENGINE ': ' Django.db.backends.mysql ', #数据库引擎
' NAME ': ' UITest ', #数据库名
' USER ': ' Root ', #数据库用户名
' PASSWORD ': ' 123456 ', #数据库密码
' HOST ': ', #数据库地址, default localhost
' PORT ': ', #数据库端口, default 3306
    }}  Configure Installed_appsAdd the app name under Installed_appsInstalled_apps = [' Django.contrib.admin ',
' Django.contrib.auth ',
' Django.contrib.contenttypes ',
' Django.contrib.sessions ',
' Django.contrib.messages ',
' Django.contrib.staticfiles ',
'webmanage',]Step Three: Create model.pyAfter class is the name of the table, inheriting models. Model, field names in the table below, fields support multiple types  From django.db Import models
# Create your models here.

class page (models. Model):
Pageid=models. Integerfield (). Primary_key
Chinessname=models. Charfield ()
Englishname=models. Charfield (). Unique
Pagedesc=models. Charfield ()

class elements (models. Model):
Elementid=models. Integerfield (). Primary_key
Pageid=models. ForeignKey (page)
Variablename=models. Charfield (). Unique
Variabledesc=models. Charfield ()
Find_by_android_option=models. Charfield ()
Find_by_android_value=models. Charfield ()
Find_by_ios_option=models. Charfield ()
Find_by_ios_value=models. Charfield () For more information on the use of models, see the official documentationhttps://docs.djangoproject.com/en/dev/topics/db/models/ Fourth Step: Synchronizing the database files Check the database fields to be modified: Python3 manage.py makemigrations after executing the command, you can see what is modified as followsAt the same time, a 000-beginning file is generated in the app's migrations, and the SQL operation is indicated in the file.   Modify Database: Python3 manage.py migrateYou will then see the new table in the database, with the table named "App name _class table name", and adding a record to the Django_migrations table Note: If Installed_apps in settings.py does not fill in the current app, can't find the models file to sync under app 

Python3 Django connection MySQL, synchronization table structure

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.