22👍
Here are all the ways to evaluate a lazy queryset. Using list
is one of them:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated
2👍
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:
try:
Catalog.objects.filter(q)[0]
except IndexError:
pass
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.
- Can not use celery delay for saved form: object is not JSON serializable
- How to nginx virtual servers + fcgi for django?
- Why "class Meta" is necessary while creating a model form?
- What does it mean for an object to be unscriptable?
- Django admin – select reverse foreign key relationships (not create, I want to add available)