[Django]-Big django queryset in Python's if conditional expression

4👍

Python will either use the __bool__ or __len__ methods to test the truth value of an object, and it looks like the implementation for the Queryset class fetches all records:

https://github.com/django/django/blob/master/django/db/models/query.py#L279

def __bool__(self):
    self._fetch_all()
    return bool(self._result_cache)

It might be a better idea to use if queryset.count() or if queryset.exists() if that’s what you want.

👤Selcuk

Leave a comment