当前位置:网站首页>ORM--查询类型,关联查询
ORM--查询类型,关联查询
2022-07-07 07:17:00 【chuntian_tester】
ORM框架中,会给每一个模型类中的主键设置一个别名:pk
models.Projects.objects.filter(id=2)等效于models.Projects.objects.filter(pk=2)
1.filter()和exclude()查询类型
exclude()就是filter()的反向查询
1.1 filter()支持的查询类型:
models.模型类名.objects.filter(字段名__查询类型=xxxx)

补充:
filter(id__gt=2)
filter(id__gte=2)
filter(id__lt=2)
filter(id__lte=2)
1.2 exclude()反向查询
filter()方法支持的查询类型都支持

2.关联查询
当一个模型类中定义了外键时,在给这个模型类对应的表新增数据时,还必须指定将从表新增的数据关联到父表的哪条数据上
2.1 在Interfaces模型类中定义外键,关联Projects模型类

2.2 迁移
迁移后,Interfaces模型类生成的表中会自动 生成外键:外键名_id

2.3 外键关联创建
方式1:
2.3.1 先获取父表模型对象,此处只能用get(),不能用filter(),为何?
2.3.2 将获取的父表模型对象以外键字段名作为参数来传递

用postman请求后查看interfaces表

方式2:
2.3.1 先获取父表模型对象,进而获取父表的id值
2.3.2 将父表 主键值 以 外键名_id 作为参数来传递


2.4 外键关联查询
2.4.1 利用从表数据获取父表数据
利用 .父表模型类名小写 获取
获取包含注册接口的所有项目信息
Interfaces.objects.filter(name__contains='注册接口').first().projects

2.4.2 利用父表数据获取从表数据
利用 从表模型类名小写_set 获取;
或者在从表模型的外键字段中,指定related_name='xxxx':
models.Projects.objects.filter(leader__contains='jeanet')[0].xxxx.all()
获取项目leader=jeanet的所有接口信息:
models.Projects.objects.filter(leader__contains='jeanet')[0].interfaces_set.all()

2.4.3 关联查询
2.4.3.1 通过父表参数来获取从表数据
从表模型类的QuerySet对象(关联属性字段__父表模型类中的字段名__查询类型=值)

2.4.3.2 通过从表参数来获取父表数据
父表模型类的QuerySet对象(从表的related_name字段名__从表模型类中的字段名__查询类型=值)

边栏推荐
- How to become a senior digital IC Design Engineer (1-6) Verilog coding Grammar: Classic Digital IC Design
- Luogu p2482 [sdoi2010] zhuguosha
- Thinkphp3.2 information disclosure
- PostgreSQL reports an error when creating a trigger,
- 位操作==c语言2
- Addition, deletion, modification and query of ThinkPHP database
- Dynamics 365online applicationuser creation method change
- phpcms实现PC网站接入微信Native支付
- The combination of over clause and aggregate function in SQL Server
- 14th test
猜你喜欢

In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen

CSDN salary increase technology - learn about the use of several common logic controllers of JMeter

Arthas simple instructions

Internship log - day07

Win10安装VS2015

CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)

小程序实现页面多级来回切换支持滑动和点击操作

小程序弹出半角遮罩层
![[bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction](/img/7f/d0917366c68865222154d82b9e7b40.png)
[bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction

“十二星座女神降临”全新活动推出
随机推荐
Guys, how can mysql-cdc convert the upsert message to append only
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
thinkphp3.2信息泄露
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
字节跳动 Kitex 在森马电商场景的落地实践
ViewPager2和VIewPager的区别以及ViewPager2实现轮播图
高斯消元
Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
20排位赛3
一大波开源小抄来袭
使用BigDecimal的坑
The combination of over clause and aggregate function in SQL Server
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
AI从感知走向智能认知
能源路由器入门必读:面向能源互联网的架构和功能
Addition, deletion, modification and query of ThinkPHP database
JS逆向教程第一发
Natapp intranet penetration
Internship log - day07
uboot机构简介