Original address:http://www.server110.com/mariadb/201404/10020.html
First, the default engine in [email protected] is django.db.backends.mysql. But in Python3 if this write, will find Django.db.backends.mysql rely on mysqldb[5], and MYSQLDB and incompatible Python3 then find a new way to continue to use MySQL.
First, according to MySQL document [3], since MySQL Connector/python 1.1.1 started, the introduction of Mysql.connector.django, can be directly used as a Django database engine, [4] but I tried 1.1.2 and 1.1.3 and found that it was not so.
Later, found Pymysql[1], support Python3, Google know how to use the combination with Django [2], the answer is as follows:
First, install Pymysql
For Windows
For Fedora
Pip-python3 Install Pymysql
|
Pip-python3 Install Pymysql |
Then, add the following two lines to the __init__.py file in the Django site:
Import Pymysql
Pymysql.install_as_mysqldb ()
|
Import Pymysql Pymysql.install_as_mysqldb () |
Finally, the configuration of the database (in settings.py)
DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ', #数据库引擎
' NAME ': ' Test ', #数据库名
' USER ': ' Root ', #用户名
' PASSWORD ': ' Root ', #密码
' HOST ': ', #数据库主机, localhost by default
' PORT ': ', #数据库端口, MySQL defaults to 3306
' OPTIONS ': {
' Autocommit ': True,
},
}
}
|
DATABASES = { ' Default ': { ' ENGINE ': ' Django.db.backends.mysql ', #数据库引擎 ' NAME ': ' Test ', #数据库名 ' USER ': ' Root ', #用户名 ' PASSWORD ': ' Root ', #密码 ' HOST ': ', #数据库主机, localhost by default ' PORT ': ', #数据库端口, MySQL defaults to 3306 ' OPTIONS ': { ' Autocommit ': True, }, } } |
Then synchronize the database:
For Windows
For Fedora
OK, It works!
How Python3 and Django support MySQL and MARIADB databases