当前位置:网站首页>ORM -- query type, association query
ORM -- query type, association query
2022-07-07 10:07:00 【chuntian_ tester】
ORM In the frame , An alias will be set for the primary key in each model class :pk
models.Projects.objects.filter(id=2) Equivalent to models.Projects.objects.filter(pk=2)
1.filter() and exclude() Query type
exclude() Namely filter() Reverse query of
1.1 filter() Supported query types :
models. Model class name .objects.filter( Field name __ Query type =xxxx)

Add :
filter(id__gt=2)
filter(id__gte=2)
filter(id__lt=2)
filter(id__lte=2)
1.2 exclude() Reverse query
filter() Methods support all query types

2. Relational query
When a foreign key is defined in a model class , When adding data to the table corresponding to this model class , You must also specify which data of the parent table to associate the new data from the table
2.1 stay Interfaces Define foreign keys in the model class , relation Projects Model class

2.2 transfer
After the move ,Interfaces The table generated by the model class will automatically Generate foreign keys : Foreign key name _id

2.3 Foreign key Association creation
The way 1:
2.3.1 First get the parent table model object , Only... Can be used here get(), Out-of-service filter(), why ?
2.3.2 Pass the obtained key field name other than the parent table model object as a parameter

use postman View after request interfaces surface

The way 2:
2.3.1 First get the parent table model object , Then get the parent table id value
2.3.2 Add parent table Primary key value With Foreign key name _id Pass... As a parameter


2.4 Foreign key Association query
2.4.1 Use to get parent table data from table data
utilize . The parent table model class name is lowercase obtain
Get all the project information including the registered interface
Interfaces.objects.filter(name__contains=' Registered interface ').first().projects

2.4.2 Use the parent table data to obtain the data from the table
utilize From the table model, the class name is lowercase _set obtain ;
Or in the foreign key field of the slave table model , Appoint related_name='xxxx':
models.Projects.objects.filter(leader__contains='jeanet')[0].xxxx.all()
Get the project leader=jeanet All interface information :
models.Projects.objects.filter(leader__contains='jeanet')[0].interfaces_set.all()

2.4.3 Relational query
2.4.3.1 Get the data from the parent table through the parent table parameters
From the table model class QuerySet object ( Associated attribute fields __ Field name in the parent table model class __ Query type = value )

2.4.3.2 Get the parent table data from the table parameters
Of the parent table model class QuerySet object ( From table related_name Field name __ From the field name in the table model class __ Query type = value )

边栏推荐
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
- Natapp intranet penetration
- China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
- Garbage disposal method based on the separation of smart city and storage and living digital home mode
- The combination of over clause and aggregate function in SQL Server
- Integer inversion
- Become a "founder" and make reading a habit
- 小程序弹出半角遮罩层
- web3.0系列之分布式存储IPFS
- 使用BigDecimal的坑
猜你喜欢
随机推荐
20排位赛3
Win10 installation vs2015
“十二星座女神降临”全新活动推出
CDZSC_ 2022 winter vacation personal training match level 21 (2)
arcgis操作:dwg数据转为shp数据
Huffman encoded compressed file
有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
根据热门面试题分析Android事件分发机制(一)
Guys, have you ever encountered the case of losing data when Flink CDC reads mysqlbinlog? Every time the task restarts, there is a probability of losing data
Luogu p2482 [sdoi2010] zhuguosha
Parameter sniffing (2/2)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
ORM--数据库增删改查操作逻辑
Natapp intranet penetration
Memory ==c language 1
Thinkphp3.2 information disclosure
14th test
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
Gym - 102219j kitchen plates (violent or topological sequence)
Write VBA in Excel, connect to Oracle and query the contents in the database









