From the documentation of exists()
Returns True
if the QuerySet contains any results, and False
if not. This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query as a normal QuerySet query.
exists()
is useful for searches relating to both object membership in a QuerySet and to the existence of any objects in a QuerySet, particularly in the context of a large QuerySet.
However they then go on to show a few examples and conclude that if qs
vs if qs.exists()
needs a large queryset for efficiency gains.
A final caveat from the documentation:
Additionally, if a some_queryset
has not yet been evaluated, but you know that it will be at some point, then using some_queryset.exists()
will do more overall work (one query for the existence check plus an extra one to later retrieve the results) than simply using bool(some_queryset)
, which retrieves the results and then checks if any were returned.