Checking for empty queryset in Django

前端 未结 7 1015
情书的邮戳
情书的邮戳 2020-12-07 07:36

What is the recommended idiom for checking whether a query returned any results?
Example:

orgs = Organisation.objects.filter(name__iexact = \'Fjuk inc\')         


        
7条回答
  •  广开言路
    2020-12-07 08:29

    The most efficient way (before django 1.2) is this:

    if orgs.count() == 0:
        # no results
    else:
        # alrigh! let's continue...
    

提交回复
热议问题