当前位置:网站首页>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循环等,每次循环返回模型对象实例
边栏推荐
- Performance optimization record of the company's product "yunzhujia"
- Pit using BigDecimal
- Create an int type array with a length of 6. The values of the array elements are required to be between 1-30 and are assigned randomly. At the same time, the values of the required elements are diffe
- Sqlplus garbled code problem, find the solution
- “十二星座女神降临”全新活动推出
- [bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
- Become a "founder" and make reading a habit
- 2020 Zhejiang Provincial Games
- 大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
- Pit encountered by vs2015 under win7 (successful)
猜你喜欢
能源路由器入门必读:面向能源互联网的架构和功能
Applet sliding, clicking and switching simple UI
细说Mysql MVCC多版本控制
Win10安装VS2015
Sqlplus garbled code problem, find the solution
Internship log - day04
Arcgis操作: 批量修改属性表
H5 web player easyplayer How does JS realize live video real-time recording?
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
Lecture 1: stack containing min function
随机推荐
14th test
Huffman encoded compressed file
Internship log - day07
[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
Software modeling and analysis
2020CCPC威海 J - Steins;Game (sg函数、线性基)
ViewPager2和VIewPager的區別以及ViewPager2實現輪播圖
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
H5 web player easyplayer How does JS realize live video real-time recording?
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
Natapp intranet penetration
Bean 作⽤域和⽣命周期
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
20排位赛3
Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
Writing file types generated by C language
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
AI从感知走向智能认知
How to solve the problem of golang select mechanism and timeout
企业实战|复杂业务关系下的银行业运维指标体系建设