restframework 使用OrderingFilter实现排序
导入
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
测试