Checking for empty queryset in Django

Niklas picture Niklas · Sep 7, 2009 · Viewed 157.3k times · Source

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

orgs = Organisation.objects.filter(name__iexact = 'Fjuk inc')
# If any results
    # Do this with the results without querying again.
# Else, do something else...

I suppose there are several different ways of checking this, but I'd like to know how an experienced Django user would do it. Most examples in the docs just ignore the case where nothing was found...

Answer

Adam picture Adam · Sep 7, 2009
if not orgs:
    # Do this...
else:
    # Do that...