1👍
It isn’t possible to use the order of the list when using pk__in
.
An alternative is to use in_bulk
to fetch all of the objects, keyed by id. Then construct an ordered list using a list comprehension.
results = [r.pk for r in search_results]
objects = MyModel.objects.in_bulk(results)
objects_in_order = [obj[pk] for pk in objects]
Source:stackexchange.com