3👍
QuerySet
s are evaluated if you "consume" the queryset. You consume a queryset by enumerating over it, call .get(…)
, .exists(…)
, .aggregate(…)
or .count(…)
, check the truthiness (for example with if myqueryset:
or bool(queryset)
, or call len(…)
over it, etc. As a rule of thumb, it gets evaluated if you perform an action on it such that the result is no longer a QuerySet
.
If you enumerate over it, or you call len(…)
the result is cached in the QuerySet
, so calling it a second time, or enumerating over it after you have called len(…)
will not make another trip to the database.
In this specific case, none of the QuerySet
s are evaluated before you make the call to the render(…)
function. If in the template you for example use {% if players %}
, or {% for players %}
, {{ players|length }}
, or {{ players.exists }}
, then it will evaluate the queryset.
1👍
Django queries are designed to be "lazy" – that is. they don’t run the database query until something requests actual data. Queries can be modified by the addition of filtering and other similar functions.
For example, the following code requests all TeamMember
objects when the search string is 'all'
, but otherwise adds a filter to restrict names to those matching the given search.
squad_list = TeamMember.objects(state__in={"Hired", "Joined", "Staff", "Recruiting"})
if squadname != 'all':
squad_list = squad_list(squad__icontains=squadname.lower())
When the squadlist
query is finally executed it will retrieve the required record. Dopes this help?
- [Django]-Setting global variables with Cactus App for Mac
- [Django]-Unicode string display on Django template
- [Django]-Django – How to show dropdown value instead object?