[Fixed]-Force evaluate a lazy query


Here are all the ways to evaluate a lazy queryset. Using list is one of them:




Using list() will evaluate the query, retrieve all of its results, and add all of the data to the QuerySet’s cache. That’s potentially a lot of overhead if you’re not actually planning to use the data.

You could instead use Catalog.objects.filter(q).exists() (see exists), which does not need to fetch the entire result set. Just note that it executes a different query, which will look something like this, depending on your DBMS:

SELECT (1) AS "a" FROM "..._catalog" LIMIT 1;

If you want to evaulate a query that is closer to what you’d originally be executing, you could do something like:

except IndexError:

That will still add a LIMIT or TOP to your query, but at least your query will still include all the columns and orderings and such that your original query would have.

You might be tempted to do something like bool(Catalog.objects.filter(q)) or otherwise use the QuerySet in a boolean context, but note that that will behave as list(), fetching the entire set of results.

Leave a comment