Django model syntax [Models]
1 Django supports Sqlite,mysql, Oracle,postgresql database by default.
<1> SQLite
Django uses SQLite's database by default, default with SQLite database driver, engine name: Django.db.backends.sqlite3
<2> MySQL
Engine Name: Django.db.backends.mysql
2 MySQL Driver
MySQLdb (MySQL python)
Mysqlclient
Mysql
Pymysql (pure python mysql driver)
Change the default database to MySQL
setttings.py
DATABASES = {' default ': { ' ENGINE ': ' Django.db.backends.mysql ', ' name ': ' Books ', # Your database name ' User ': ' Root ', # your database username ' PASSWORD ': ', # Your database password ' host ': ', # Your database host, leave blank default to localhost ' Port ': ' 3306 ', # Your database port }}
Attention:
Name is the name of the database, the database must have been created before the MySQL connection, and the db.sqlite3 under the SQLite database above is automatically created by the project
User and password are the username and password of the database respectively.
Once setup is complete, we need to activate our MySQL before starting our Django project.
Then, start the project and will error: no module named MySQLdb
This is because Django defaults to the driver you are importing is MYSQLDB, but MySQLdb is a big problem for py3, so the driver we need is pymysql, that is, we just need to find the __init__ under the project name file and write it inside:
Import Pymysql
Pymysql.install_as_mysqldb ()
Problem Solving!
Python Learning---Django model syntax 180124