At the beginning of this chapter, let’s install mysql

Install MySQL with apt

  • 1. Update the source

  • 2. Install mysql

  • 3. Perform initial configuration
sudo mysql_secure_installation
Copy the code

  • Check the mysql service status
systemctl status mysql.service
Copy the code

The ORM profile

  • ORM, which means object-relation Mapping in Chinese, is produced with the development of object-oriented software development methods. Object-oriented development method is the mainstream development method in today’s enterprise application development environment, and relational database is the mainstream data storage system that permanently stores data in enterprise application environment. Objects and relational data are two representations of business entities, which are represented as objects in memory and relational data in a database. There are associations and inheritance relationships between objects in memory, but in database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, object-relational mapping ORM systems generally exist in the form of middleware, mainly to achieve the mapping of program objects to relational database data. Object orientation is developed from the basic principles of software engineering (such as coupling, aggregation, encapsulation), while relational database is developed from mathematical theory. There are significant differences between the two theories. In order to solve this mismatch, object-relational mapping technology came into being. The O/R derives from “Object” and the R from “Relational.” In almost all programs, there are objects and relational databases. In the business logic layer and the user interface layer, we are object-oriented. When the object information changes, we need to store the object information in a relational database. Currently popular ORM products such as Java Hibernate,.NET EntityFormerWork and so on.
  • ORM is included in the Model module of MVC framework, which brings the following benefits to developers
  1. The data model is decoupled from the database, and the database can be easily replaced with a simple configuration without code changes
  2. You only need object-oriented programming, you don’t need to write database oriented code
  • Classes defined in Model in MVC correspond to tables in relational database through ORM, and the attributes of objects reflect the relations between objects, which are also mapped to data tables

Create project test2

Mysql installation package

pip3 install pymysql
Copy the code

  • Open a new terminal, log in to mysql on the cli, and create database test2
  • Note: Set the character set to UTF8
create database test2 charset=utf8;
Copy the code

  • Go back to the first terminal and create project test2 in the /home/desktop/pytest directory
django-admin startproject test2
Copy the code

  • Open the test2/settings.py file and locate the DATABASES TAB. By default, the SQLite3 database is used
  • Change to use the MySQL database as follows
  • Change the engine to mysql, and provide the connected HOST HOST, PORT PORT, database Name Name, USER Name USER, and PASSWORD PASSWORD

  • Go to the test2 directory and create the application booktest
cd test2
python manage.py startapp booktest
Copy the code

This error occurs when you type this command

Add the following code to the init.py file

  • To install the booktest application into your project, open the test2/settings.py file, find the INSTALLED_APPS TAB, and add the following code
'booktest',
Copy the code