[Django]-Django: Grab a set of objects from ID list (and sort by timestamp)

33👍

This can be done using such a code:

objects = Model.objects.filter(id__in=object_ids).order_by('-timestamp')

the order_by can be positive or negative timestamp, depending how you want it sorted.

👤kender

10👍

Try the following:

result = Model.objects.filter(id__in=object_ids)

This returns all Model objects that have their id in the given list of object_ids. This way, you also don’t need to append additional models to the resulting QuerySet.

Leave a comment