当前位置:网站首页>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循环等,每次循环返回模型对象实例
边栏推荐
- thinkphp3.2信息泄露
- 2020ccpc Weihai J - Steins; Game (SG function, linear basis)
- CDZSC_2022寒假个人训练赛21级(2)
- PostgreSQL创建触发器的时候报错,
- 哈夫曼编码压缩文件
- uboot机构简介
- 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
- [original] what is the core of programmer team management?
- How will fashion brands enter the meta universe?
- [untitled]
猜你喜欢
细说Mysql MVCC多版本控制
Internship log - day07
“十二星座女神降临”全新活动推出
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
Garbage disposal method based on the separation of smart city and storage and living digital home mode
Applet sliding, clicking and switching simple UI
js逆向教程第二发-猿人学第一题
What development models did you know during the interview? Just read this one
Sqlplus garbled code problem, find the solution
[original] what is the core of programmer team management?
随机推荐
How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
【原创】程序员团队管理的核心是什么?
ViewPager2和VIewPager的区别以及ViewPager2实现轮播图
Check the example of where the initialization is when C initializes the program
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
C# XML的应用
剑指 Offer II 107. 矩阵中的距离
web3.0系列之分布式存储IPFS
Application of C # XML
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
中国首款电音音频类“山野电音”数藏发售来了!
一大波开源小抄来袭
Thinkphp3.2 information disclosure
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力
Use 3 in data modeling σ Eliminate outliers for data cleaning
企业实战|复杂业务关系下的银行业运维指标体系建设
The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart