基本查询:
book = BookInfo.objects.get(btitle=‘?西游记‘) #单?一查询,如果结果不不存在报错
book = BookInfo.objects.all(btitle=‘?西游记‘) #查询多个结果,有多少返回多
少,不不存在返回None
book = BookInfo.objects.filter(btitle=‘?西游记‘).count() #查询结果的数量
book = BookInfo.objects.exclude(btitle=‘?西游记‘) #查询结果取反
模糊查询:
contains 是否包含
startswith,endswith 以指定值开头或结尾
空查询:
isnull 是否为空
范围查询:
in 在范围内
range 相当于between...and...
book = BookInfo.objects.get(btitle=‘?西游记‘) #单?一查询,如果结果不不存在报错
book = BookInfo.objects.all(btitle=‘?西游记‘) #查询多个结果,有多少返回多
少,不不存在返回None
book = BookInfo.objects.filter(btitle=‘?西游记‘).count() #查询结果的数量
book = BookInfo.objects.exclude(btitle=‘?西游记‘) #查询结果取反
book = BookInfo.objects.filter(btitle__contains=‘记‘) #查询结果包
含‘记’
%记%
book = BookInfo.objects.filter(btitle__startswith=‘?西‘) #查询以‘?西’开
头的 ?西%
book = BookInfo.objects.filter(btitle__endswith=‘记‘) #查询以‘记’结尾
的 %记
book = BookInfo.object.filter(bititle__isnull=Flase) #查询bititle不为空
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 不不等于
F对象和Q对象
?比较两个字段对象之间的关系?用F对象。(F对象可以进?行运算)
与逻辑运算符连?用使?用Q对象。 或( | ) 与( & ) ?( ~ )
聚合函数
使?用aggregate()过滤器?调?用聚合函数。聚合函数包括:Avg 平均,Count 数量,Max 最?,
Min 最?小,Sum 求和
排序
使?用order_by对结果进?行行排序
book = BookInfo.object.filter(id__gt =10) #查询id?大于10的
book = BookInfo.object.exclude(id = 10) #查询id不不等于的10的
book = BookInfio.Object.filter(bread__gte=F(‘bcomment‘)) #查询阅读量等
于评论量量的对象
book = BookInfio.Object.filter(bread__gte=F(‘bcomment‘) * 2 )
book = BookInfo.Object.filter(Q(bread__gte=20) | Q(pk__lt=3)) #查询阅
读量量为20或者id为3的对象
book = BookInfo.Object.aggregate(Sum(‘bread‘)) #求阅读量的和
book=BookInfo.object.all().order_by(‘bread‘) #按阅读量的升序排列
book=BookInfo.object.all().order_by(‘-bread‘) #按阅读量的降序排列
关联查询
?一对多模型
?一到多的访问语法:?一对应的模型类对象.多对应的模型类名小写_set
(?一本书里有多个英雄,?一个英雄只能存在?一本书里。表关系为?一对多,英雄表?里外键关联书
id,英雄表里的存放多个书id。英雄表为多,书表为?一。)
多到?一的访问语法:多对应的模型类对象.多对应的模型类中的关系类属性名
?方向查询除了了可以使?用模型类名_set,还有?一种是在建立模型类的时候使?用related_name来
指定变量量名。
多对多操作
增加
b = BookInfo.object.filter(id = 1)
b.heroinfo_set.all() #查询book_id = 1的书?里的所有英雄
h = HeroInfo.object.filter(id = 1)
h.hbook #查询英雄id = 1的书是哪本。
hbook=model.ForeignKey(HeroInfo,on_delete=model.CACADE,null=Ture,relat
ed_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)
原文:https://www.cnblogs.com/quanliangxu/p/14032713.html