This article mainly for you in detail the Django configuration MySQL database detailed steps, with a certain reference value, interested in small partners can refer to
The Django Project uses the SQLite database by default, but I want to use the MySQL database and how it should be configured.
The operation of the Django connection MySQL database is implemented through the configuration of the root module, in the configuration file settings.py of the project root module, we can query the configuration information of the following databases:
DATABASES = {' default ': { ' ENGINE ': ' Django.db.backends.sqlite3 ', ' NAME ': Os.path.join (Base_dir, ' Db.sqlite3 '), }}
Explain the meaning of the above code:
Engine: For the configuration of a particular database engine, different database fields are common:
Django.db.backends.postgresql # PostgreSQLdjango.db.backends.mysql # mysql django.db.backends.sqlite3 # SQLite django.db.backends.oracle # Oracle
Name: Refers to the name of the database to connect to
Django's documentation shows that if you want to use MySQL, you need to install a Python db API DRIVER that is the database interface driver, commonly used database driver interface has three kinds, mysqldb, Pymysql, Mysqlclient. But MySQLdb does not support Python3, and the official recommendation is to use Mysqlclient. So this article also uses mysqlclient. Okay, scrap number is not much to say, start today's Django config MySQL tour.
First step: download Mysqlclient
Pip Install Mysqlclient
The second step: Create a database, such as I created a blog database, you can create with commands, you can also use the visualizer (Navicat Premium) to create, command to create DATABASE code:
CREATE DATABASE Blog (database_name) CHARACTER SET UTF8; #指定数据库的编码utf8
Step Three: configure the MySQL connection parameters in settings.py as follows:
DATABASES = {' default ': ' ENGINE ': ' Django.db.backends.mysql ', ' NAME ': ' blog ', ' USER ': ' Root ', ' PASSWORD ': ' kong1234, ', ' HOST ': ' 127.0.0.1 ', ' PORT ': ' 3306 ', }}
User: Database login username, mysql is usually root
PASSWORD: The password for the login database must be the password for the user
Host: Because the general database is the C/s structure, so you have to specify the location of the database server, we generally database server and client are on a host, so the general default is filled 127.0.0.1
Port: Database server ports, MySQL defaults to 3306
Both the host and port can be blank, using the default configuration, but if you change the default configuration, you will need to fill in the changed
In fact, this is basically done, in order to better illustrate, give an example to demonstrate the operation of the data.
Fourth Step: Create the Model class models.py in the app you created.
For example, I am in the application of my blog, models.py create a class, the code is as follows:
From __future__ import unicode_literalsfrom django.db import Modelsclass article (models. Model): title = models. Charfield (max_length=32, default= ' Title ') content = models. TextField (Null=true)
Fifth Step: map The fields into a data table and execute the following command
Python manage.py makemigrations (application name, optional) Python manage.py migrate (application name, optional)
Sixth step: execute python manage.py sqlmigrate app name file ID
View SQL statements
Python manage.py sqlmigrate Blog 0001
Specific Django database operations can refer to the next blog post.