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