Take any given queryset, qs = QS.objects.filter(active=True)
Is there i difference between:
if qs:
and
if qs.exists():
regarding load on the db, etc?
Yes, there's a difference:
if qs
will use the __nonzero__
method of the QuerySet
object, which calls _fetch_all
which will in turn actually execute a full query (that's how I interpret it anyway).exists()
does something more efficient, as noted by Ewan. That's why this method... exists.So, in short, use exists()
when you only need to check for existence since that's what it's for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With