Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

It may be also good to evaluate all at once using list() or any other method of eval of the query. I find it to boost performance sometimes (no paying for the DB connections every time).

Find more info about when django evaluates here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top