[7.1] Django-Introduction to ORM and the installation and use of MySQL

1. Introduction to ORM

Object-Relational Mapping ( Object Relational the Mapping , referred to as the ORM , or O / RM , or O / R & lt Mapping ), is a programming technique for implementing object-oriented programming languages of different types of systems to convert between data. That is to say, we no longer need to rely on a specific database when designing a data model. You can easily replace the database with some simple configurations, reducing the workload of developers, and do not need to face invalid labor caused by data changes.

1.1 The role of ORM

  • Establish the corresponding relationship between the model class and the table, allowing us to operate the database in an object-oriented way
  • Generate tables in the database according to the designed model class
  • The database can be switched through a simple configuration

1.2 Advantages of ORM

  • Only need object-oriented programming, no need to write code to the database
  • Operations on the database are transformed into operations on class attributes and methods
  • No need to write SQL statements for various databases
  • Realize the decoupling of the data model and the database, shielding the differences in the operation of different databases
  • Do not pay attention to the internal details of databases such as Mysql and Oracle
  • You can easily replace the database through a simple configuration, without the need to modify the code

1.3 Disadvantages of ORM

  • ORM can only perform operations on the corresponding data table (add, delete, modify, and check)
  • Unable to operate the database, we need to create a database in advance
  • For complex business, the use cost is higher
  • According to the operation of the object, it is converted into a SQL statement, and according to the result of the query, it is converted into an object. There is a performance loss in the mapping process.

1.4 ORM in Django

# 在Django的ORM中通过类来对应sql的表,一个类对应创建一个表
class Book(models.Model):
    pass

Two, MySQL download and installation

Three, DJango connects to the database

Since Djangothe ORMcomponents inside cannot create and manage the database itself, you can only rely on the local database to create a database, connect through the Djangomiddle settings.py, and then you can ORMcreate related tables in the database to increase the table structure. Delete, modify, and check.

3.1 MySQL new database

3.1.1 MySQL set default encoding

  • utf8It uses 1~4a byte, a variable length encoding format, character encoding
  • mb4It most bytes 4means that it is used to be compatible with four bytes unicode, that is, use 4one byte to represent the completeUTF-8
  • utf8mb4Yes, it is utf8a superset. No other conversion is needed except changing the encoding to utf8mb4. In order to save space, it utf8is enough under normal circumstances . To Mysqlsave the 4byte length of UTF-8characters, you need to use utf8mb4the character set, but only the 5.5.3later versions is supported. In order to obtain better compatibility, you should always be used utf8mb4instead utf8. For CHARthe type of data, utf8mb4it will consume some space, according to Mysqlofficial suggested the use of VARCHARalternative CHAR.

Set the encoding when creating the database

Permanently modify the default character set to utf8mb4

[Introduction to some configuration items of my.ini] [win10 mysql8 my.ini configuration file]
  • C:\Program Files\MySQL\MySQL Server 8.0Create a my.inifile in the directory
  • ② Open with Notepad and modify the configuration file as:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
# basedir=E:\Software\mysql-8.0.19-winx64
# 设置mysql数据库的数据的存放目录,在安装mysql-5.7.30-winx64.zip版本的时候,
# 此配置不可添加,否则mysql将无法启动。
# datadir=E:\Software\mysql-8.0.19-winx64\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
# 关闭ssl
# skip_ssl
# 配置时区
default-time_zone='+8:00'
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
  • utf8Change to utf8mb4, now there are more character sets, restart the mysqlserver
win + r
services.msc
# 管理员模式下运行
net stop mysql80
net start mysql80
  • ③ Check whether the character set is changed successfully
# 进入到mysql的bin目录下
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
# 登录mysql
mysql -u root -p
# 然后输入密码
# 查看字符集是否修改成功
# 方式一
mysql> \s
# 方式二
mysql> show variables like 'character%';

3.1.2 MySQL new database

  • cd MySQLpath (into the installation MySQLof binthe)
  • mysql -u root -p( MySQLEntry to start )
  • ③ Enter the set password and log in
  • ④Database create database orm_test(create database)
  • show databases(View all the created databases, and check whether the database is created successfully)

3.2 Django connects to MySQL database

  • In settings.py, modify the default DATABASES, the code is as follows:
DATABASES = {
    'default':{
        'ENGINE':'django.db.backends.mysql', # 设置Django连接的数据库类型
        'NAME':'orm_test', # 需要连接的数据库名称,连接前需要先创建好
        'USER':'root', # 连接数据库的用户名
        "PASSWORD":'123456', # 连接数据库的密码
        'HOST':'127.0.0.1', # 连接主机,默认为本机
        'PORT':3306, # 连接的端口号,默认为3306
    }
}
  • After modifying the default configuration, the connection can be successful, of course, you can also connect to other available databases on the server

3.3 ORM mapping in Django

  • Model is a Pythonclass, it is a subclass Django.db.models.Modelderived from
  • A model class represents a data table in the database
  • Each class attribute in the model class represents a field in the database
  • Model is the interface of data interaction, the method and way of expressing and data