https://www.bilibili.com/video/BV1z64y197Dk?p=20&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea
views.py
from rest_framework.filters import OrderingFilter
class BookView(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializers
#添加默认的排序功能
filter_backends = [OrderingFilter]
#可排序的字段指定
ordering_fields=("id","price")
其实就是加上
#添加默认的排序功能
filter_backends = [OrderingFilter]
#可排序的字段指定
ordering_fields=("id","price")
使用方式
http://127.0.0.1:8000/book/?ordering=-price -为倒叙