其他分享
首页 > 其他分享> > 排序

排序

作者:互联网

from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
from app01.models import Book
from app01.ser import BookSerializer
class Book2View(ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ('id', 'price')
    
# urls.py
path('books2/', views.Book2View.as_view()),
]

# 使用:
http://127.0.0.1:8000/books2/?ordering=-price
http://127.0.0.1:8000/books2/?ordering=price
http://127.0.0.1:8000/books2/?ordering=-id
```

  

标签:127.0,http,ordering,0.1,books2,import,排序
来源: https://www.cnblogs.com/dzs894330350/p/16104795.html