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.
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.
- [Django]-Django : extract a path from a full URL
- [Django]-Google app engine + python (django) deployment error: Error loading MySQLdb module
- [Django]-How to use Apache to serve Django server and React client?
- [Django]-How to access the parent model of a Django-CMS plugin
- [Django]-Chords of Chords Timeout
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.