首页 > 其他分享 >django查询优化之是否存在

django查询优化之是否存在

时间:2023-03-27 21:57:21浏览次数:34  
标签:count exists res queryset 查询 data print django 优化

res = Booking.objects.filter()
# 方法一:exists()
if res.exists():
    print('queryset has data')
else:
    print('queryset has no data')

# 方法二:count() ==0
if res.count() == 0:
    print('empty')

# 方法三:
if res:
   print('queryset has data')
else:
    print('queryset has no data')

总之,queryset.exists() > queryset.count() > queryset

性能比是exists()最佳,而直接用queryset来判断是最差

 

标签:count,exists,res,queryset,查询,data,print,django,优化
From: https://www.cnblogs.com/shaoyishi/p/17263119.html

相关文章