Search code examples
sortingdjango-rest-frameworkdjango-filter

Django rest framework - add secondary order to OrderingFilter


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?


Solution

  • According to the documentation, you can set default order using ordering param with several fields

    class YourListView(generics.ListAPIView):
        ...
        ordering = ['close_date', 'id']