Django Connection MySQL

Source: Internet
Author: User

The problem has arisen several times. Each time with a new person, will ask, with the old driver will also ask. Last weekend, the ox sister also asked. This morning, I will summarize the operation process.

Modify a database connection

First of all, in the settings.py file, is there a piece of content:

The Django project was created automatically when it was built. This is to tell you that the Django default connection is Sqllite. ENGINE: Refers to the name of the database driver connected, name refers to what library to connect, what file. In fact, the engine has the following conditions:

Django.db.backends.postgresql Connecting PostgreSQL

Django.db.backends.mysql connecting MySQL

Django.db.backends.sqlite3 Connect SQLite

Django.db.backends.oracle Connecting Oracle

If you want to connect MySQL and the like, need the account password, the connection configuration should be written like this:

DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ',
' NAME ': ' Myspinach ',
' USER ': ' XXXXXXX ',
' PASSWORD ': ' XXXXXX ',
' HOST ': ' 127.0.0.1 ',
' PORT ': ' 3306 ',
}
}

Name: refers to database names

User and Password: Index login account and password

HOST: Refers to the database server address

Next, you should install the database driver

Note that in python2.x, everyone is using: MySQLdb. But Python3.x's players don't use this anymore. Use: Pymysql. The installation mode is the same:

Then, create the table and sync to MySQL:

In Django, there are several ways to operate the MySQL database. Today, we summarize the way we use Django Orm.

First, you have to create a model: Note that it needs to be from models. Model inheritance

To perform a migration command:
First command: Create a migration file

Second command: Synchronizing to a database

Look at the results:

OK, we're going to be here today. Tomorrow, we will explain how to use ORM for database additions and deletions, and database one-to-many relationships

Django Connection MySQL

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.