F查询:取出数据库的某个字段的值
# 把read_num都加1 from django.db.models import F ret=models.Book.objects.all().update(read_num=F('read_num')+1) print(ret) #查询评论数大于阅读数的书籍 ret=models.Book.objects.all().filter(commit_num__gt=F('read_num')) for i in ret: print(i.name) ## 查询评论数大于阅读数2倍的书籍 ret=models.Book.objects.filter(commit_num__gt=F('read_num')*2) print(ret)
# Q查询:制造 与或非的条件
# Q查询:制造 与或非的条件
# 查询名字叫egon或者价格大于100的书
from django.db.models import Q # ret=models.Book.objects.filter(Q(name='egon') | Q(price__gt=100)) # 查询名字叫egon并且价格大于100的书 # ret=models.Book.objects.filter(Q(name='egon') & Q(price__gt=100)) # ret=models.Book.objects.filter(name='egon',price__gt=100) # 查询名字不为egon的书 # ret = models.Book.objects.filter(~Q(name='egon')) # print(ret) # Q可以嵌套 ret = models.Book.objects.filter((Q(name='egon') & Q(price__lt=100)) | Q(id__lt=3)) print(ret)
标签:__,Book,models,ret,egon,objects,查询 From: https://www.cnblogs.com/97zs/p/17868011.html