Thanks! Would this still give 404 errors when a certain object does not exist?
On Tuesday, 2 June 2015 17:07:49 UTC+1, Simon Charette wrote: > > Hi Cherie, > > A `id__in` queryset lookup should issue a single query. > > levels = Level.objects.filter(id__in=level_ids) > > Cheers, > Simon > > Le mardi 2 juin 2015 11:42:19 UTC-4, Cherie Pun a écrit : >> >> Hi, >> >> I am new to Django and I am performing some iteration on a queryset. When >> I use django_debug_toolbar to look at the SQL usage, I realised that Django >> is actually calling the database once in each iteration. Is there a way to >> make it call the database only once and somehow store it locally so that I >> can iterate on it? >> >> Code: >> for level_id in levels_id: >> levels.append(get_object_or_404(Level, id=level_id)) >> >> >> I am trying this modified code, but it seems that it's still calling the >> same number of times. >> >> levels_dict = repr(Level.objects.values('id', 'name')) >> for level_id in levels_id: >> levels.append(levels_dict.get(id=level_id).get('name')) >> >> I am trying to use this pattern in quite a few places, where I want to >> get some data from a list of objects retrieved from the database and >> iterate over them. >> I would be happy to provide any missing information. >> >> Could anyone kindly help me out? Cheers! >> >> -- 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 django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. 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/844b1d2b-8665-4176-bee5-06297ebe7247%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.