book = BookInfo.objects.get(btitle='⻄游记') #单⼀查询,如果结果不存在报错
book = BookInfo.objects.all() #查询多个结果,有多少返回多少,不存在返回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='记')#查询以‘记’结尾的 %记
空查询:
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(