Django Learning: Mysql database connection

Development Environment:

                  Windows 10

                  Python 3.7.4

                  Django 2.2.6

                  Mysql 8.0.17

Continuing with the previous one: https://www.cnblogs.com/daydayupup/p/11741873.html

Database Configuration

Django default sqlite3 database, which is an embedded database, it is a database file. Since SQLite itself is written in C, and the volume is very small, therefore, often it integrated into a variety of applications. Since Python itself built sqlite3, so no need to install any program, you can use it directly.

If you want to connect Mysql database, then it would download the appropriate database driver modules, and make appropriate changes in seetings.py file.

First, open CMD , that use pip download Mysql database driver module, I use a mysqlclient

pip  install mysqlclient

Then open mysite / seeting.py file, using "Ctrl + F" Search key combination "DATABASES", by default in the case shown in FIG.

 Configure MySQL database

 

Note: You need to pre-create a database, Django will not automatically help you create the database. Of course, if you are using sqlite3 you do not need to do any pre-configured.

Next, search for "INSTALLED_APPS", register here your APP.

Registration APP role is to make your APP can be used by other projects, or can be packaged and distributed to others to use in their projects.

Other entries above contains all the applications in your Django project will use, these applications are enabled by default as a convenience to bring regular projects. Some applications enabled by default requires at least a data table, so you need to create some tables in the database before you use them:

py manage.py migrate

 

You can use the command line to list the database tables Django created

Create a model

Model is simple and clear description of real data. It contains the necessary data storage fields and behavior. Django follows the DRY Principle . Its goal is that you only need to define the data model, and other assorted code that you do not care, they will be automatically generated from the model.

Django to define the class from the Python to define the specific form of the model, the physical model of the present embodiment is a Python each class Class, each model represents a table in the database, each instance representing the class data table row data, each variable in the class data table representative of a field. By Django model, the Python code database operations and combined to achieve encapsulation of the SQL query language. That is, you can not manage the database, SQL language can not, you can the same Python code to operate the database.

In polls / models , the two new models, respectively, and Question Chocie. Question containing a question and a publication date. Choice contains two fields: the number of votes the text description of the option and the option. Each Choice is associated to a Question. These are embodied by a Python class, all written in Python code, do not touch any SQL statement. Now, edit the polls/models.pyfile, the specific code as follows:

 

#polls/models

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question,on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

 

  • In the above code, each class is django.db.models.Modela subclass.
  • Each field is Field instance of a class, for example, a character field is denoted as CharField, expressed as a time field DateTimeField. This tells Django data type for each field to be processed.
  • Each Field name of the class instance variables (such as: question_text) is the name of the field, therefore, to meet the variable naming database naming format.
  • Definitions for certain Field class instance requires parameters, such as a need question_text max_length parameter. The usefulness of this parameter is not only used to define the database structure, is used for data validation.
  • Field instance of the class can accept a plurality of optional parameters, such as defaul = 0 is an optional parameter.
  • Note that in the end, we use the ForeignKeydefinition of a relationship. This tells Django, each Choiceobject is associated to a Questiontarget. Django supports all the common database relationships: many-to-many and one to one.

 Activation model

py manage.py makemigrations polls

Then, you will see the following output

By running the command, Django will you detect changes to the model file, which tells Django model changes, and the changes to be saved as a migration (migration). makemigrations 

Migration (migration) is stored in the form of changes to the Django model definition (that is, the database structure). To put it plainly, the migration file is the data you want to make changes to the database stored in the behavior .

We can in polls / migrations / 0001_initial.py view the migration of data we have just recorded, it can also be modified manually.

data migration

Next, we use the migrate command to the database of real data migration:

py manage.py migrate

The migrate command to select all the migration has not been executed (migration) (Django by creating a special table in the database  django_migrations to keep track of which performed the migration) and applications on the database - that is, you change the model to synchronize database structure on.

Data migration is broken down into two commands are generated and applied in order to allow you to submit data on migration code control system and enable it to be used in multiple applications in; this will not only make development easier, but also to other developers and use the production environment convenience.

Test1 mysql command checks using the database table, as shown below:

This shows that data migration success!

Can also be seen from a database table is named class name APP name _models defined .

 

Guess you like

Origin www.cnblogs.com/daydayupup/p/11742121.html