I am using Django for my website, and hence decided to use Django Rest Framework for building my REST APIs. For a particular model, i want to filter on a text field (using SearchFilter for that), filter on a few categorical fields (FilterBackend with a FilterSet defined) and be able to order data based on some fields (OrderingFilter for this).
class StatsAPI(generics.ListAPIView):
model = Stats
queryset = Stats.objects.all()
serializer_class = StatsSerializer
filter_backends = (filters.DjangoFilterBackend, filters.OrderingFilter, filters.SearchFilter)
filter_class = StatsFilter
pagination_class = StatsPagination
ordering_fields = ('__all__')
search_fields = ('display_name')
The issue i am facing is with my ordering fields as they also contain nulls. Ordering in ascending order works fine. However ordering in descending order (www.example.com/api/stats/?ordering=-appearance), pushes the null values to the top.
How do i ignore the null values when using descending order? The number of fields on which ordering can be performed are roughly 20 in number.
The NULLS FIRST and NULLS LAST options can be used to determine whether nulls appear before or after non-null values in the sort ordering. By default, null values sort as if larger than any non-null value; that is, NULLS FIRST is the default for DESC order, and NULLS LAST otherwise.
How do i pass on this value from django or django rest framework? – Tutelage