Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
When are Django Querysets executed in the view?

I read the Querysets Django docs regarding querysets being lazy, but still am a bit confused here.

So in my view, I set a queryset to a variable like so

players = Players.objects.filter(team=team)

Later on, I have a sorting mechanism that I can apply

sort = '-player_last_name'


if pts > 20:
    players = players.filter(pts__gte = pts).order_by(sort)
else:
    players = players.filter(pts__lte = pts).order_by(sort)

if ast < 5:
   players = players.filter(asts__lte = ast).order_by(sort)
else:
   players = players.filter(asts__gte = ast).order_by(sort)

context = {players: players)

return render(request, '2021-2022/allstars.html', context)

What I want to know is, when is the players queryset evaluated? is it when each page is rendered, or everytime I assign the queryset to a variable? Because if it's the former, then I can just apply the .order_by(sort) chain and the previous applications are redundant.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

QuerySets 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 QuerySets 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.

over 4 years ago · Santiago Trujillo Denunciar

0

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?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda