Hi,

I am new to Django and I am trying to reduce the number of calls to 
database as it's slowing down the app. I am performing iteration over the 
queryset and I used django_debug_toolbar to check the number of queries 
made, and the number is huge. It looks like django is making a query call 
to the db in each iteration. Is there a way to make it only send one query 
and then store the result locally and iterate over it?

Code:
for level_id in levels_id:
            levels.append(get_object_or_404(Level, id=level_id))

I have been trying out this code, but it seems that the number of sql 
queries is still the same.
levels_dict = Level.objects.values('id', 'name')
        for level_id in levels_id:
            levels.append(levels_dict.get(id=level_id).get('name'))

I am using the same pattern in a few places to get data from each of the 
object in the queryset, is there a way to improve the performance? I am 
happy to provide any missing information. Cheers!

P.S. I tried to post just now but the post seems to be missing so I am 
trying again, hopefully I didn't post twice!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3745df48-5278-4b59-ad1e-f03ba855da15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to