Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
Do using queryset.count() caches the queryset?

I'm making some filtering in my endpoint, and one of the filters are only applied if the filtered queryset has more than 30 items.

yesterday_date = timezone.now() - timezone.timedelta(days=1)
if query_dict.get("active"):
    active_query = cleaned_query.filter(created_at__gt=yesterday_date)
    if active_query.count() > 30:
        cleaned_query = active_query
    else:
        cleaned_query = cleaned_query[:30]

My doubt is, will the .count() method already evaluates and caches the queryset or should I use len(queryset) to avoid another database hit in case it's bigger than 30?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you check django's docs about When Querysets Are Evaluated

You'll see some information about counting...

Note: If you only need to determine the number of records in the set (and don’t need the actual objects), it’s much more efficient to handle a count at the database level using SQL’s SELECT COUNT(*). Django provides a count() method for precisely this reason.

So a count() doesn't evaluate the set, whereas a len() does.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda