当前位置:网站首页>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循环等,每次循环返回模型对象实例

边栏推荐
- What development models did you know during the interview? Just read this one
- [4G/5G/6G专题基础-146]: 6G总体愿景与潜在关键技术白皮书解读-1-总体愿景
- Lecture 1: stack containing min function
- Esp8266 uses TF card and reads and writes data (based on Arduino)
- Internship log - day04
- There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
- Bean 作⽤域和⽣命周期
- AI从感知走向智能认知
- 请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
- 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
猜你喜欢
![[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](/img/21/6a183e4e10daed90c66235bdbdc3bf.png)
[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

arcgis操作:dwg数据转为shp数据

Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion

Oracle installation enhancements error

Natapp intranet penetration

官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展

基础篇:带你从头到尾玩转注解

基于智慧城市与储住分离数字家居模式垃圾处理方法

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

Pytest learning - dayone
随机推荐
CDZSC_ 2022 winter vacation personal training match level 21 (2)
[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
【原创】程序员团队管理的核心是什么?
请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
JS逆向教程第一发
Huffman encoded compressed file
Diffusion模型详解
thinkphp数据库的增删改查
Win10安装VS2015
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Communication mode between processes
The combination of over clause and aggregate function in SQL Server
Applet sliding, clicking and switching simple UI
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
中国首款电音音频类“山野电音”数藏发售来了!
PostgreSQL创建触发器的时候报错,
字节跳动 Kitex 在森马电商场景的落地实践
Three years after graduation
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
PostgreSQL reports an error when creating a trigger,