This period of time in learning Mysql+django knowledge points. Take a note of the pits and experiences of the following learning process.
The tool used is navicat for MySQL
Python 2.7.12
Mysql-python 1.2.3.
First create a new database (Students_info) in the secondary directory, note the user name, password, port ... This database is subsequently docked in settings.py.
Django and database docking operation flow:
1, you have to tell the Django database information, the following framework will automatically go to the database based on this information to access the databases do not worry about you.
The code is as follows:
DATABASES = {
' Default ':
{
' ENGINE ': ' Django.db.backends.mysql ',
' NAME ': ' Students_info ',
' USER ': ' Root ',
' PASSWORD ': ' 123456 ',
' HOST ': ' 127.0.0.1 ',
' PORT ': ' 3306 ',
}
}
The above is the setting in settings.py, yes it is so simple. Engine according to a variety of databases can refer to the official document https://docs.djangoproject.com/en/1.7/ref/settings/#std: setting-databases.
2, define the data model, this work is in the corresponding app (blog) application directory in the model.py completed. Examples are as follows (djangobook example):
Class Publisher (models. Model): name = models. Charfield (max_length=30) address = models. Charfield (max_length=50) City = models. Charfield (max_length=60) state_province = models. Charfield (max_length=30) country = models. Charfield (max_length=50) website = models. Urlfield () class Author (models. Model): first_name = models. Charfield (max_length=30) last_name = models. Charfield (max_length=40) email = models. Emailfield () class book (Models. Model): title = models. Charfield (max_length=100) authors = models. Manytomanyfield (Author) publisher = models. ForeignKey (Publisher) publication_date = models. Datefield ()
Each class corresponds to a table.
3. The next step is to run Python manage.py makemigrations blog
Then run Python manage.py migrate
If there is a problem with the second run, you can remove the blog/migrates under 0001_initial.py and rerun the above two commands.
4. If no problem, you can see the created table in Navigcat.
Django+mysql Study Notes