代码:
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
class TestListAPIView(ListAPIView):
queryset = models.Course.objects.filter(is_delete=False).all()
serializer_class = serializers.TestModelSerializer
# 配置过滤器类
filter_backends = [OrderingFilter,]
# 参与排序的字段: ordering=-score, id
ordering_fields = ['id', 'score']
前端请求:
http://127.0.0.1:8000/api/test?ordering=-score,id