Django learning record: Use ORM to operate MySQL database and complete data addition, deletion, modification and query
Database operations
MySQL database+pymysql
Django development and operation of the database are simpler, and an ORM framework is provided internally.
Install third-party modules
pip install mysqlclient
What ORM can do:
1. Create, modify, and delete tables in the database (without writing SQL statements). [Unable to create database]
2. Manipulate the data in the table (no need to write SQL statements).
1. Create your own database
1) Start the MySQL service
2) Bring your own tools to create a database
2. Django connects to the database
Configure and modify in the setting.py file: the local mysql database is connected here
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname', # 数据库名字
'USER': 'root',
'PASSWORD': 'xxxxxx',
'HOST': 'localhost',
'PORT': 3306,
}
}
3. Django operation table
-
create table
-
Delete table
-
Modify table
Create table: In models.py file:
class UserInfo(models.Model):
name = models.CharField(max_length=32) # charfield 字符串类型
password = models.CharField(max_length=64)
age = models.IntegerField() # IntegerField 整数类型
Equivalent to running in MySQL
create table app01_userinfo(
id bigint auto_increment primary key,
name varchar(32),
password varchar(64),
age int
);
Then, execute the commands sequentially in the terminal: (Note: the app needs to be registered in advance.)
python manage.py makemigrations
python manage.py migrate
You can create the app01_userinfo file in mysql
*When adding a column to the table, since there may be data in the existing column, the new column must specify the data corresponding to the new column:
-
Enter a value manually
-
Set default value
size = models.IntegerField(default=2)
-
Allow empty
data = models.IntegerField(null=True, blank=True)
If you want to adjust the table structure during development:
-
Just operate the class in the models.py file.
-
Execute the command in the terminal
python manage.py makemigrations python manage.py migrate
Addition, deletion, modification and query of data
from app01 import models
1. Create new
# ###新建###
models.Department.objects.create(title="销售部")
models.Department.objects.create(title="IT部")
models.Department.objects.create(title="运营部")
models.UserInfo.objects.create(name="dumpling", password="123", age="22")
models.UserInfo.objects.create(name="noodles", password="111", age="20")
2. Delete
# ###删除###
models.UserInfo.objects.filter(id=3).delete()
models.Department.objects.all().delete()
3. View (obtain data)
###获取数据###
#获取的是列表,列表是一行一行的数据
#data_list = [行(对象) 行 行] QuerySet类型
data_list = models.UserInfo.objects.all()
for obj in data_list:
print(obj.id, obj.name, obj.password, obj.age)
# 寻找id=1的数据。data_list = [对象,],这个方法取到的还是QuerySet类型
data_list = models.UserInfo.objects.filter(id=1)
# 取对象中的第一个,这个方法就能直接将第一行对象取出来
row_obj = models.UserInfo.objects.filter(id=1).first()
print(row_obj.id, row_obj.name, row_obj.password, row_obj.age)
4. Update
models.UserInfo.objects.all().update(password=999)
models.UserInfo.objects.filter(id=2).update(age=999)