paginations.py文件:自定以加密分页类
from rest_framework.pagination import CursorPagination
class CarCursorPagination(CursorPagination):
# 默认一页的条数
page_size = 3
# 用户可以自定义选择一页的条数,但最多显示max_page_size设置的条数
page_size_query_param = 'page_size'
max_page_size = 5
# 默认排序规则:按pk从小到大排序,-pk表示从大到小排序
ordering = 'pk'
view.py文件:
from rest_framework.viewsets import ModelViewSet
from . import models, serializer
from .paginations import CarCursorPagin
from rest_framework.filters import OrderingFilter
class CarModelViewSet(ModelViewSet):
queryset = models.Car.objects.filter(is_delete=False)
serializer_class = serializer.CarModelSerializer
pagination_class = CarCursorPagination
# filter_backends = [OrderingFilter]
总结:
1.采用默认排序访问,url链接:car/?cursor=加密串
2.结合视图类实现OrderingFilter自定义排序访问,url链接:car/?cursor=加密串&ordering=排序字段