Django-based ORM database configuration

Source: Internet
Author: User

1. Create your own database manually

Create DATABASE name

2. Configure the database-related configuration in the Django project under the setting file

1DATABASES = {2     'default': {3         'ENGINE':'Django.db.backends.mysql',#Types of connection Databases4         'HOST':'127.0.0.1',#Connect to database address5         'PORT': 3306,#Port number6         'NAME':'Manage',#Database name7         'USER':'Kevin',#Database user name8         'PASSWORD':'12121',#Database Password9     }Ten}

3. Tell Diango to use Pymysql instead of MYSQLDB to connect to the database

Configure non-app _init_.py in the project's _init_.py file

1 Import Pymysql 2 # let Django use Pymysql instead of MYSQLDB. 3 pymysql.install_as_mysqldb ()

4. The model file in the app defines class (the Mapping database table name), and the class must inherit models. Model

The object of the class corresponds to a record of the table, and the attribute corresponds to a column record;

1 class UserInfo (models. Model):2     # self-increment type primary key 3     # not empty char length

5. Execute a command statement to create a data table

>>>python manage.py makemigrations

>>>python manage.py Migrate

Django-based ORM database configuration

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.