Federer Federer - 24 days ago 4
SQL Question

When Does Django Perform the Database Lookup?

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?


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?


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


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.