当前位置:网站首页>ORM模型--数据记录的创建操作,查询操作
ORM模型--数据记录的创建操作,查询操作
2022-07-07 07:17:00 【chuntian_tester】
1.数据库表记录的创建
1.1 通过模型类名(字段1=值1,字段2=值2.。。).save()新增记录


1.2 通过模型类名.objects.create(字段1=值1,字段2=值2.。。。)新增记录


2.数据库表记录的查询
2.1 查询多条数据
2.1.1 查询数据库中所有数据
qs = 模型类.objects.all(),获取到的是一个查询集queryet对象
a).模型类.objects.all(),返回QuerySet对象,查询集
b).使用模型类.objects.all(),会将当前模型类对应的数据表中的所有数据读取出来
c).QuerySet类似列表,具有惰性查询的特点,在“用”数据时才执行Sql语句

2.2 查询单条数据
2.2.1 模型类.objects.get()
这种方法最好使用唯一约束的字段去查询
a).如果使用get中的指定条件查询记录数量=0或>1,都会抛出异常
b).如果使用get中的指定条件查询记录数量=1,会返回这条记录的模型实例对象,
可以通过 实例对象.字段名 去获取对应字段的值

2.2.2 模型类.objects.fillter()
a).可以使用模型类.objects.filter(条件1=值1),返回QuerySet对象
b).如果使用指定条件查询记录数量=0,返回空的QuerySet对象
c).如果使用指定条件查询记录数量>1,将符合条件的模型对象实例包裹到QuerySet
对象中返回
d).QuerySet对象,类似列表,有如下特性:
支持通过正整数索引取值
支持切片操作
获取第一个模型对象:QuerySet对象.first()
获取最后一个模型对象:QuerySet对象.last()
获取长度:len(QuerySet对象), QuerySet对象.count()
判断查询集QuerySet对象是否为空:QuerySet对象.exists()
支持列表的迭代操作,for循环等,每次循环返回模型对象实例

边栏推荐
- ViewPager2和VIewPager的区别以及ViewPager2实现轮播图
- Bean 作⽤域和⽣命周期
- 网上可以开炒股账户吗安全吗
- Oracle installation enhancements error
- How to use Mongo shake to realize bidirectional synchronization of mongodb in shake database?
- 终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
- Thinkphp3.2 information disclosure
- 14th test
- Difference between process and thread
- iNFTnews | 时尚品牌将以什么方式进入元宇宙?
猜你喜欢

小程序实现页面多级来回切换支持滑动和点击操作
![[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision](/img/fd/5e8f74da25d9c5f7bd69dd1cfdcd61.png)
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision

Natapp intranet penetration

Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat

Flex flexible layout

First issue of JS reverse tutorial

How to use clipboard JS library implements copy and cut function

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

Win10安装VS2015

Delete a record in the table in pl/sql by mistake, and the recovery method
随机推荐
高斯消元
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
Check the example of where the initialization is when C initializes the program
JS reverse tutorial second issue - Ape anthropology first question
AI从感知走向智能认知
Do you have a boss to help look at this error report and what troubleshooting ideas are there? Oracle CDC 2.2.1 flick 1.14.4
使用BigDecimal的坑
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
一大波开源小抄来袭
Three years after graduation
Pytest learning - dayone
Luogu p2482 [sdoi2010] zhuguosha
C# XML的应用
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
Oracle installation enhancements error
Thinkphp3.2 information disclosure
2020 Zhejiang Provincial Games
MongoDB创建一个隐式数据库用作练习
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform