Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 455285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:21:19+00:00 2026-05-12T22:21:19+00:00

From the following code: dvdList = Dvd.objects.filter(title = someDvdTitle)[:10] for dvd in dvdList: result

  • 0

From the following code:

dvdList = Dvd.objects.filter(title = someDvdTitle)[:10]

for dvd in dvdList:
    result = "Title: "+dvd.title+" @ "+dvd.price+"."

When does Django do the lookup? Maybe it’s just paranoia, but it seems if I comment out the for loop, it returns a lot quicker. Is the first line setting up a filter and then the for loop executes it, or am I completely muddled up? What actually happens with those lines of code?

EDIT:

What would happen if I limited the objects.filter to ‘1000’ and then implemented a counter in the for loop that broke out of it after 10 iterations. Would that effectively only get 10 values or 1000?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:21:19+00:00Added an answer on May 12, 2026 at 10:21 pm

    Django querysets are evaluated lazily, so yes, the query won’t actually be executed until you try and get values out of it (as you’re doing in the for loop).

    From the docs:

    You can evaluate a QuerySet in the following ways:

    Iteration. A QuerySet is iterable, and
    it executes its database query the
    first time you iterate over it. For
    example, this will print the headline
    of all entries in the database:

    for e in Entry.objects.all():
        print e.headline
    

    …(snip)…

    See When Querysets are evaluated.

    Per your edit:

    If you limited the filter to 1000 and then implemented a counter in the for loop that broke out of it after 10 iterations, then you’d hit the database for all 1000 rows – Django has no way of knowing ahead of time exactly what you’re going to do with the Queryset – it just knows that you want some data out of it, so evaluates the query string it’s built up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does this line from the following code sample mean? synchronized (_surfaceHolder) { _panel.onDraw(c);
my browser is showing this as a result from the following code (code comes
I have got the following code from here to read an Excel file using
When I try to run the following code (from the REPL) in Clojure: (dotimes
The following code should find the appropriate project tag and remove it from the
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
So I have the following code: return from a in DBContext.Acts join artist in
I'm using the following code to query a database from my jsp, but I'd
I ran across the following code in Ely Greenfield's SuperImage from his Book component
I'm using the following code within the JCProperty class to retrieve data from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.