当前位置:网站首页>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字段名__从表模型类中的字段名__查询类型=值)

边栏推荐
- HCIP 第一天 笔记整理
- CDZSC_2022寒假个人训练赛21级(1)
- Lecture 1: stack containing min function
- sql 里面使用中文字符判断有问题,哪位遇到过?比如value<>`无`
- CDZSC_ 2022 winter vacation personal training match level 21 (1)
- [bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
- C# XML的应用
- Detailed explanation of diffusion model
- [untitled]
- 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...)
猜你喜欢

【原创】程序员团队管理的核心是什么?

Software modeling and analysis

Pit encountered by vs2015 under win7 (successful)

iNFTnews | 时尚品牌将以什么方式进入元宇宙?

Sqlplus garbled code problem, find the solution

Strategic cooperation subquery becomes the secret weapon of Octopus web browser

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

PLC信号处理系列之开关量信号防抖FB

H5 web player easyplayer How does JS realize live video real-time recording?
![[untitled]](/img/5b/61efbaded29250bc8d921b0cf087c8.png)
[untitled]
随机推荐
Software modeling and analysis
Main (argc, *argv[]) details
[untitled]
字节跳动 Kitex 在森马电商场景的落地实践
Applet sliding, clicking and switching simple UI
JS逆向教程第一发
Switching value signal anti shake FB of PLC signal processing series
为什么安装mysql时starting service报错?(操作系统-windows)
[original] what is the core of programmer team management?
There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart
How to use clipboard JS library implements copy and cut function
Check the example of where the initialization is when C initializes the program
农牧业未来发展蓝图--垂直农业+人造肉
Pit encountered by vs2015 under win7 (successful)
HCIP 第一天 笔记整理
大佬们,有没有遇到过flink cdc读MySQLbinlog丢数据的情况,每次任务重启就有概率丢数
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
小程序滑动、点击切换简洁UI
MongoDB创建一个隐式数据库用作练习