Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!