当前位置:网站首页>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 )

边栏推荐
- 哈夫曼编码压缩文件
- [4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development
- Natapp intranet penetration
- ES类和对象、原型
- 虚数j的物理意义
- Switching value signal anti shake FB of PLC signal processing series
- Luogu p2482 [sdoi2010] zhuguosha
- CDZSC_2022寒假个人训练赛21级(1)
- 小程序弹出半角遮罩层
- CDZSC_ 2022 winter vacation personal training match level 21 (1)
猜你喜欢

ORM模型--数据记录的创建操作,查询操作
![[untitled]](/img/5b/61efbaded29250bc8d921b0cf087c8.png)
[untitled]

Google Colab装载Google Drive(Google Colab中使用Google Drive)

Enterprise practice | construction of banking operation and maintenance index system under complex business relations

基础篇:带你从头到尾玩转注解

Sqlplus garbled code problem, find the solution

小程序弹出半角遮罩层

The landing practice of ByteDance kitex in SEMA e-commerce scene

农牧业未来发展蓝图--垂直农业+人造肉

A wave of open source notebooks is coming
随机推荐
Hcip first day notes sorting
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
Why does the starting service report an error when installing MySQL? (operating system Windows)
洛谷P2482 [SDOI2010]猪国杀
为什么安装mysql时starting service报错?(操作系统-windows)
Check the example of where the initialization is when C initializes the program
flink. CDC sqlserver. 可以再次写入sqlserver中么 有连接器的 dem
Applet sliding, clicking and switching simple UI
Web3.0 series distributed storage IPFs
网上可以开炒股账户吗安全吗
位操作==c语言2
Sword finger offer II 107 Distance in matrix
Flex flexible layout
Pytest learning - dayone
Write VBA in Excel, connect to Oracle and query the contents in the database
A wave of open source notebooks is coming
Interface test
Pit using BigDecimal
2020浙江省赛