Django REST框架 – 在OrderingFilter中添加次要排序

huangapple go评论42阅读模式
英文:

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']

huangapple
  • 本文由 发表于 2023年4月17日 14:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定