Skip to content Skip to sidebar Skip to footer

Using List Comprehension Instead Of For Loop When Working With Django Querysets

I'm trying to tweak an application that is suffering in speed department. Because of that, I've started converting all the for-loop statements to list comprehensions when possible.

Solution 1:

Your problem isn't for loops vs. list comprehensions (better would be generators). Your problem is way too many queries to the database.

Since you're trying to get one list, you should be trying to get it from one query. If you explained what was in your typical QuerySet, we could show you how best to merge them. Maybe use OR merges on Q objects. Or maybe building up a set of integers and feeding it to an __in= filter.

Solution 2:

Your solution does not work because you're creating a list of querysets. Each value is a queryset, and you join them nowhere. Compare:

a = [1,2,3]
b = [x for x in a if x ]

You would expect b to be a list of integers as well, right? You get the list of querysets (better than the list of parents), but you still need to join them. You can do this using itertools.chain:

http://docs.python.org/library/itertools.html#itertools.chain

foos = itertools.chain(booms)

foos should be what you want.

Solution 3:

Agree it's tough to tell what's going on exactly, but debugging django speed issues is made vastly easier by using the django debug toolbar:

https://github.com/django-debug-toolbar/django-debug-toolbar

If the issue is too many db hits, watch the "SQL queries" tab on the toolbar, it will show it very clearly.

Post a Comment for "Using List Comprehension Instead Of For Loop When Working With Django Querysets"