zoukankan      html  css  js  c++  java
  • drf分页器

    1 分页器

    # 查所有,才需要分页
    from rest_framework.generics import ListAPIView
    # 内置三种分页方式
    from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
    
    '''
    PageNumberPagination
        page_size:每页显示的条数
    '''
    
    
    class MyPageNumberPagination(PageNumberPagination):
        # http://127.0.0.1:8000/api/books2/?aaa=1&size=6
        page_size = 3  # 每页条数
        page_query_param = 'aaa'  # 查询第几页的key(url中)
        page_size_query_param = 'size'  # 每一页显示的条数
        max_page_size = 5
    
    
    class MyLimitOffsetPagination(LimitOffsetPagination):
        default_limit = 3  # 每页条数
        limit_query_param = 'limit'  # 往后拿几条
        offset_query_param = 'offset'  # 标杆
        max_limit = 5  # 每页最大几条
    
    
    class MyCursorPagination(CursorPagination):
        cursor_query_param = 'cursor'  # 每一页查询的key
        page_size = 3  # 每页显示的条数
        ordering = '-id'  # 排序字段
    
    
    class BookPaginationView(ListAPIView):
        queryset = models.Book.objects.all()
        serializer_class = BookModelSerializer
        # 配置分页
        # pagination_class = MyPageNumberPagination
        # pagination_class = MyLimitOffsetPagination
        pagination_class = MyCursorPagination
        
    # 如果使用APIView分页
    from utils.throttling import MyThrottle
    
    
    class BookPaginationAPIView(APIView):
        # 局部配置
        # throttle_classes = [MyThrottle,]
    
        def get(self, request, *args, **kwargs):
            book_list = models.Book.objects.all()
            # 实例化得到一个分页器对象
            page_obj = MyPageNumberPagination()
            book_list = page_obj.paginate_queryset(book_list, request, view=self)
            nex_url = page_obj.get_next_link()
            pr_url = page_obj.get_previous_link()
            print(nex_url)
            print(pr_url)
            book_ser = BookModelSerializer(book_list, many=True)
            return Response(data=book_ser.data)
    
    #settings.py
    REST_FRAMEWORK={
        'PAGE_SIZE': 2,
    }
    
  • 相关阅读:
    页面返回劫持js代码
    js向input赋值
    JavaScript中统计Textarea字数并提示还能输入的字符
    dedecms 列表页调用文章列表时对有无缩略图进行判断调用
    常用mate标签-常用mate标签
    dedecms修改arc.listview.class.php实现列表页支持mip
    extend简单用法
    splice从数组中删除指定定数据
    递归【输入一个日期】返回【前12个月每月最后一天】
    三步把asp.net core 3.1应用部署到centos7
  • 原文地址:https://www.cnblogs.com/h1227/p/13305545.html
Copyright © 2011-2022 走看看