排序
OrderingFilter类支持简单的查询参数控制结果排序
- 配置
class GameView(CustomModelViewSet):
queryset = Game.objects.all()
serializer_class = GameSerializer
filter_backends = (DjangoFilterBackend, filters.SearchFilter,filters.OrderingFilter)
# 过滤
# filter_fields = ('name', 'status')
filterset_class = GameFilter
# 搜索
search_fields = ("name", "status")
#排序
#注意 filter_backends多了一个filters.OrderingFilter
ordering_fields = ['status', "id", "name"]
- 降序
http://127.0.0.1:8000/api/v1/games/?ordering=-id # 根据id降序
- 升序
http://127.0.0.1:8000/api/v1/games/?ordering=status # 根据状态升序