[Fixed]-Reverse Queryset Order in Django

37👍

You can use

queryset = reversed(Collection.objects.filter(pk__in = li))

or

queryset = Collection.objects.filter(pk__in = li).reverse()

9👍

To reverse qs

queryset = Collection.objects.filter(pk__in = li).order_by('field_name')

If for some reason you don’t want to use order_by then reverse list

queryset = Collection.objects.filter(pk__in = li)
result = reversed(list(queryset))

Leave a comment