Let's say that I have a list of movie ids in a QuerySet in the following format:
movie_ids = [12,4,9,16]
Now, I want to pull the list of corresponding movies. I also want to minimize the number of queries. So I do the following:
movies = Movie.objects.filter(pk__in=movie_ids)
However, this query won't guarantee that movies will have the corresponding movie objects in the same order as they were in movie_ids, will it? Is there anyway to guarantee the order (so that each movie_id corresponds to the correct movie) so I can do something pair the two lists into a dictionary with:
id_movies = dict(zip(movie_ids, movies))
This would then be used in my template in Django.