英文:
Django rest framework - add secondary order to OrderingFilter
问题
我正在使用DRF的OrderingFilter的ordering_fields来对我的ListAPIView进行排序:
ordering_fields = ('close_date')
问题是,有些行的'close_date'相同,但每次排序的顺序都不一样,因此我想在每次调用时添加第二排序。因此,在每个查询中,数据将按'close_date'和'id'进行排序。
我该如何实现这个功能?
英文:
I'm using DRF OrderingFilter ordering_fields for sorting my ListAPIView:
ordering_fields = ('close_date')
The problem is that there are rows with the same 'close_date' and the order is not the same each time, therefore I want to add secondary sort for each call. so in each query the data will be sorted by 'close_date' AND 'id'
How can I implement this?
答案1
得分: 0
根据文档,您可以使用ordering
参数设置默认排序,其中包括多个字段。
class YourListView(generics.ListAPIView):
...
ordering = ['close_date', 'id']
英文:
According to the documentation, you can set default order using ordering
param with several fields
class YourListView(generics.ListAPIView):
...
ordering = ['close_date', 'id']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论