Django ten-connect to mysql database and create data table

One, django model

Django provides good support for various databases, including: PostgreSQL, MySQL, SQLite, Oracle.

Django provides a unified calling API for these databases. We can choose different databases according to our business needs.

MySQL is the most commonly used database in web applications.

In this blog, we will use Mysql as an example to introduce.

If you want to learn more about the basics of MySQL, you can view the rookie tutorial address: https://www.runoob.com/mysql/mysql-tutorial.html

 

Second, the installation of the third-party library mysqlclient

Details: Python needs to install a third-party library [mysqlclient] to operate the mysql database. The third-party library [mysqlclient] is actually a mysql driver.

1. The first step: Use the command [pip install mysqlclient] to download and install the latest version of mysqlclient.

2. Step 2: Next, use the command [pip list] to view the current version number of mysqlclient.

Three, the complete steps of python operation mysql (assuming that this django project [helloworld] is a brand new project and a database has not been created)

1. The first step: we need to create a new database in the local mysql database, such as a new database named [hongjingsheng_project].

detail:

Because the orm framework that comes with django can only manipulate data tables, not databases, we need to manually create the database with the mysql command line or with the navicate tool on the local computer (I chose to create it with the navicate tool).

2. Step 2: Next, we check the path in the django project [helloworld] as [helloworld/helloworld/settings.py] The default data provided by the system of the attribute [DATABASES] in the py file.

 Details: For the specific use of the attribute [DATABASES], you can check the official django document address: https://docs.djangoproject.com/en/3.1/ref/settings/#databases

3. Step 3: Because our django project [helloworld] needs to use the mysql database as a database for storing project data, we then need to set the path in the django project [helloworld] to [helloworld/helloworld/settings.py] this py The attribute [DATABASES] value in the file is changed as follows.

The data type of the attribute [DATABASES] is a dictionary, in the attribute [DATABASES] [DATABASES["default"]["ENGINE"]], this [ENGINE] means: the name of the connection database driver. The name has the following situations:

  • django.db.backends.postgresql connect to the database PostgreSQL
  • django.db.backends.mysql connect to the database mysql
  • django.db.backends.sqlite3 connect to the database sqlite
  • django.db.backends.oracle connect to the database oracle

4. Step 4: In the django project [helloworld], we create a new application [hello] through the command line (how to operate this step is mentioned in the previous blog, the application [hello] has been created, here it is No need to recreate it).

5. Step 5: We add the application [hello] to the property INSTALLED_APPS in the path [helloworld/helloworld/settings.py] in the django project [helloworld], which means that the django framework is in the django project [helloworld] Such an application [hello]. (In the previous blog, it was mentioned how to operate this step. The application [hello] has been added to the attribute INSTALLED_APPS, so there is no need to repeat it here).

6. Step 6: Modify the code content in models.py corresponding to the path [helloworld/hello/models.py] in the django project [helloworld].

Details: As you can see from the screenshot above, we have created a new subclass Person. The subclass Person inherits from the parent class models.Model. This subclass Person has attributes name (name means name) and attribute age (age means age).

Two classes are used here: class CharField and class IntegerField. More use of this class will be written in follow-up related blogs.

Note: If you want to create multiple data tables, you can create multiple classes with different class names in models.py. For specific operations, please refer to the official document address: https://docs.djangoproject.com/en/3.1/intro /tutorial02/

7. Step 7: Create a related table structure/table in a specified database through the related command line (Note: The table structure is actually a table, and the name is different). (In the actual development process, [3.7. Step 7] This step can be ignored, but in order to write the complete operation process, this step probably writes some main operations.)

7.1. First, before creating any application table structure, we can see that there is only one [__init__.py] in the migrations folder of the path [helloworld/hello/migrations] corresponding to the django project [helloworld].

7.2. Next, we are under the path [helloworld/],

7.2.1. First, suppose that we incorrectly fill in the value of ['ENGINE'] as ['django.db.backends.'] in the script file [settings.py], and then execute the command line [python manage.py migrate ]. ([3.7.2.1] This part of the content is just to teach you how to simply locate the problem according to the error log, you can ignore it)

After the terminal executes the command: python manage.py migrate:

7.2.2. Suppose we fill in the value of ['ENGINE'] in the script file [settings.py] as ['django.db.backends.mysql'], and then execute the command line [python manage] successfully for the first time .py migrate].

Reported an error:

Solve the problem that Django executes manage.py prompts NameError: name'_mysql' is not defined

The reason is that:

Mysqldb is not compatible with python3.5 and later versions

Solution:

Use pymysql instead of MySQLdb

step:

  • Install pymysql: pip install pymysql
  • Open the project in setting.py of the init .py, or directly at the very beginning of the current py file and add the following:
import pymysql 

pymysql.install_as_MySQLdb()

A good way to specify the version directly is simpler than other solutions:

import pymysql

pymysql.version_info = (1, 4, 13, "final", 0)

pymysql.install_as_MySQLdb()

After successfully executing [python manage.py migrate]:

Details: These logs indicate that the relevant data tables have been successfully generated for us in the specified database.

8. In order to let the django framework know that we have some changes in the models.py in a specific application, we need to do the following operations.

We now use an application [hello] as the data for the next explanation.

8.1. First, we execute the command line [python manage.py makemigrations hello] under the path [helloworld/].

detail:

(1) To migrate the data table information of a specified application name, the command line is [python manage.py makemigrations application name].

(2) The function that this command line [python manage.py makemigrations hello] will achieve after successful execution is: it will only generate related migration files in the file named [migrations] in the only file in this [hello] application. These The migration file involves the data table information that needs to be added and modified, but it will not perform the operation of generating the relevant data table in the specified database! (These migration files will execute the operation of creating related data tables in the specified database after the successful execution of a new command mentioned in the [3.9] chapter!)

(3) To migrate the data table information of all applications in a specified django project, for example, to migrate the data table information of all applications in our django project [helloworld], the command line is [python manage.py makemigrations].

9. Step 9: Next, in order to let the django framework know which application we want to add and modify the data table, we need to do the following operations.

We still use an application [hello] as the data for the next explanation.

9.1. First, we execute the command line [python manage.py migrate hello] under the path [helloworld/].

detail:

(1) To create the latest data table information contained in the specified application name in the specified database, the command line is [python manage.py migrate application name].

(2) The function of this command line [python manage.py migrate hello] after successful execution is: generate the latest data contained in an application named [hello] in the database named [hongjingsheng_project] in the specified database Table information. )

(3) To generate the data table information of all applications in a specified django project in the specified database, for example, to generate the data table information of all applications in our django project [helloworld], the command line is [python manage.py migrate]

detail:

(1) The value rule for the table name of the generated data table is: application name_class name (note: the first uppercase letter of the class name will be converted to lowercase letters) (such as: hello_person).

(2) , even though we did not models.py in [hello] application in person to the table set the primary key, but django framework automatically adds an id as the primary key person table.

 (3), [python manage.py makemigrations] and [python manage.py makemigrations application name] and [python manage.py migrate] and [python manage.py migrate application name], the more specific use of these four command lines and The difference can be seen in the blog address written by this big cow: https://blog.csdn.net/hpu_yly_bj/article/details/78928089

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/114303365