Django get a QuerySet from array of id's in specific order
I don't think you can enforce that particular order on the database level, so you need to do it in python instead.
id_list = [1, 5, 7]objects = Foo.objects.filter(id__in=id_list)objects = dict([(obj.id, obj) for obj in objects])sorted_objects = [objects[id] for id in id_list]
This builds up a dictionary of the objects with their id as key, so they can be retrieved easily when building up the sorted list.