Django的ORM中如何判断查询结果是否为空,判断django中的orm为空

 

 

result= Booking.objects.filter()


#方法一 .exists()
if result.exists():
    print "QuerySet has Data"
else:
    print "QuerySet is empty"
#方法二 .count()==0
if result.count() == 0:
    print "empty"
#方法三
if result:
    print "QuerySet has Data"
else:
    print "QuerySet is empty"

总结:
QuerySet.exists() > QuerySet.count()==0 > QuerySet

  

posted @ 2017-07-20 16:43  淋哥  阅读(19170)  评论(1编辑  收藏  举报