导入

from rest_framework.filters import OrderingFilter

使用

class CourseListApiView(ListAPIView):
    # 指定过滤和排序类
    filter_backends = [DjangoFilterBackend, OrderingFilter,]
    # 指定过滤字段
    filter_fields = ('course_category',)
    # 指定排序字段
    ordering_fields = ('id', 'students', 'price')
    queryset = Course.objects.filter(is_show=True).order_by('orders', '-id')
    serializer_class = CourseSerializer

测试

restframework 使用OrderingFilter实现排序

 

相关文章:

  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-11-09
  • 2021-11-02
  • 2022-12-23
猜你喜欢
  • 2022-01-15
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2021-11-04
相关资源
相似解决方案