[Django]-Using list comprehension instead of for loop when working with Django QuerySets

5đź‘Ť

âś…

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.

👤Mike DeSimone

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.

👤markijbema

1đź‘Ť

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.

👤Dan Ancona

Leave a comment