ORM操作
ORM操作
book = BookInfo.objects.get(btitle=‘⻄游记’) #单⼀查询,如果结果不存在报错
book = BookInfo.objects.all(btitle=‘⻄游记’) #查询多个结果,有多少返回多
少,不存在返回None
book = BookInfo.objects.filter(btitle=‘⻄游记’).count() #查询结果的数量
book = BookInfo.objects.exclude(btitle=‘⻄游记’) #查询结果取反
模糊查询:
- contains 是否包含
book = BookInfo.objects.filter(btitle__contains=‘记’) #查询结果包含‘记’
startswith,endswith 以指定值开头或结尾
book = BookInfo.objects.filter(btitle__startswith=‘⻄’) #查询以‘⻄’开头的 ⻄%
book = BookInfo.objects.filter(btitle__endswith=‘记’) #查询以‘记’结尾的 %记
空查询:
- isnull 是否为空
book = BookInfo.object.filter(bititle__isnull=Flase) #查询bititle不为空
- 范围查询:
in 在范围内
range 相当于between…and…
book = BookInfo.object.filter(id__in = [1,5,13,24]) #查询id为1或5或13或24
book = BookInfo.object.filter(id__range = [10,20]) #查询范围为10-20的id
- ⽐较查询:
- gt ⼤于
gte ⼤于等于
lt ⼩于
lte ⼩于等于
exclude 不等于
book = BookInfo.object.filter(id__gt =10) #查询id⼤于10的
book = BookInfo.object.exclude(id = 10) #查询id不等于的10的
- F对象和Q对象
⽐较两个字段对象之间的关系⽤F对象。(F对象可以进⾏运算)
book = BookInfio.Object.filter(bread__gte=F(‘bcomment’)) #查询阅读量等于评论量的对象
book = BookInfio.Object.filter(bread__gte=F(‘bcomment’) * 2 )
与逻辑运算符连⽤使⽤Q对象。 或( | ) 与( & ) ⾮( ~ )
book = BookInfo.Object.filter(Q(bread__gte=20) | Q(pk__lt=3)) #查询阅读量为20或者id为3的对象
- 聚合函数
使⽤aggregate()过滤器调⽤聚合函数。聚合函数包括:Avg 平均,Count 数量,Max 最⼤,Min 最⼩,Sum 求和
book = BookInfo.Object.aggregate(Sum(‘bread’)) #求阅读量的和
- 排序
使⽤order_by对结果进⾏排序
book=BookInfo.object.all().order_by(‘bread’) #按阅读量的升序排列
book=BookInfo.object.all().order_by(’-bread’) #按阅读量的降序排列
- 关联查询
⼀对多模型
⼀到多的访问语法:⼀对应的模型类对象.多对应的模型类名⼩写_set
b = BookInfo.object.filter(id = 1)
b.heroinfo_set.all() #查询book_id = 1的书⾥的所有英雄
(⼀本书⾥有多个英雄,⼀个英雄只能存在⼀本书⾥。表关系为⼀对多,英雄表⾥外键关联书id,英雄表⾥的存放多个书id。英雄表为多,书表为⼀。)
- 多到⼀的访问语法:多对应的模型类对象.多对应的模型类中的关系类属性名
h = HeroInfo.object.filter(id = 1)
h.hbook #查询英雄id = 1的书是哪本。
⽅向查询除了可以使⽤模型类名_set,还有⼀种是在建⽴模型类的时候使⽤related_name来指定变量名
hbook=model.ForeignKey(HeroInfo,on_delete=model.CACADE,null=Ture,related_name=‘heros’)
b.herose.all()
- 多对多操作
- 增加
class ManyToManyTest(APIView):
def post(self, request):
⽅法⼀:在建⽴manytomany的models⾥添加数据,(⼀条,⼀个对象)
teacherobj = models.Teacher.objects.filter(id=1).first()
studentobj = models.Student.objects.filter(id=2).first()
teacherobj.stu.add(studentobj)
return Response({“status”: 200 })
#⽅法⼆:在未建⽴manytomany的models⾥添加数据,(⼀条,⼀个对象)
teacherobj = models.Teacher.objects.all()
studentobj = models.Student.objects.filter(id=2).first()
studentobj.teacher_set.add(*teacherobj)
return Response({
“status”: 200
})
删除
class ManyToManyTest(APIView):
def delete(self, request):
⽅法⼀:在建⽴manytomany的models⾥删除数据,(⼀条,⼀个对象)
teacherobj = models.Teacher.objects.filter(id=1).first()
studentobj = models.Student.objects.filter(id=2).first()
teacherobj.stu.remove(studentobj)
return Response({
“status”: 200
})
#⽅法⼆:在未建⽴manytomany的models⾥删除数据,(多条,可迭代对象)
teacherobj = models.Teacher.objects.all()
studentobj = models.Student.objects.filter(id=2).first()
studentobj.teacher_set.remove(*teacherobj)
return Response({
“status”: 200
})
修改
class ManyToManyTest(APIView):
def put(self, request):
⽅法⼀:在建⽴manytomany的models⾥修改数据,参数只能是可迭代对象
teacherobj = models.Teacher.objects.filter(id=3).first()
studentobj = models.Student.objects.filter(id=2)
teacherobj.stu.set(studentobj)
return Response({
“status”: 200
})
#⽅法⼆:在未建⽴manytomany的models⾥修改数据,参数只能是可迭代对象
teacherobj = models.Teacher.objects.all()
studentobj = models.Student.objects.filter(id=2).first()
studentobj.teacher_set.set(*teacherobj)
return Response({
“status”: 200
})
查看
class ManyToManyTest(APIView):
def get(self, request):
⽅法⼀:在建⽴manytomany的models⾥查数据
teacherobj = models.Teacher.objects.get(id=2)
data = teacherobj.stu.all()
data_list = []
for i in data:
data_dic={
“student_name”:i.name,
“teacher_name”:teacherobj.name
}
data_list.append(data_dic)
return Response(data_list)
⽅法⼆:在未建⽴manytomany的models⾥查数据
studentobj = models.Student.objects.get(id=2)
data = studentobj.teacher_set.all()
data_list = []
for i in data:
data_dic = {
“student_name”: studentobj.name,
“teacher_name”: i.name
}
data_list.append(data_dic)
return Response(data_list)