Reverse Queryset Order in Django [duplicate]

2020-04-07 02:47发布

Is there any simple way to reverse the order of a queryset in Django?

Example:

li = [1, 2, 3]

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

2条回答
不美不萌又怎样
2楼-- · 2020-04-07 03:17

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))
查看更多
仙女界的扛把子
3楼-- · 2020-04-07 03:25

You can use

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

or

queryset = Collection.objects.filter(pk__in = li).reverse()
查看更多
登录 后发表回答