zoukankan      html  css  js  c++  java
  • 利用rest-framework实现django应用的分页功能

    自定义分页的类,继承 PageNumberPagination

    class StandardResultsSetPagination(PageNumberPagination):
        page_size = 100
        page_size_query_param = 'page_size'
        max_page_size = 1000

     在某个视图下应用自定义分页类

    class BillingRecordsView(generics.ListAPIView):
        queryset = Billing.objects.all()
        serializer_class = BillingRecordsSerializer
        pagination_class = LargeResultsSetPagination

    或者全局应用自定义分页类

    REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'apps.core.pagination.StandardResultsSetPagination'
    }

    配置含义

    The PageNumberPagination class includes a number of attributes that may be overridden to modify the pagination style.
    
    To set these attributes you should override the PageNumberPagination class, and then enable your custom pagination class as above.
    
        django_paginator_class - The Django Paginator class to use. Default is django.core.paginator.Paginator, which should be fine for most use cases.
        page_size - A numeric value indicating the page size. If set, this overrides the PAGE_SIZE setting. Defaults to the same value as the PAGE_SIZE settings key.
        page_query_param - A string value indicating the name of the query parameter to use for the pagination control.
        page_size_query_param - If set, this is a string value indicating the name of a query parameter that allows the client to set the page size on a per-request basis. Defaults to None, indicating that the client may not control the requested page size.
        max_page_size - If set, this is a numeric value indicating the maximum allowable requested page size. This attribute is only valid if page_size_query_param is also set.
        last_page_strings - A list or tuple of string values indicating values that may be used with the page_query_param to request the final page in the set. Defaults to ('last',)
        template - The name of a template to use when rendering pagination controls in the browsable API. May be overridden to modify the rendering style, or set to None to disable HTML pagination controls completely. Defaults to "rest_framework/pagination/numbers.html".

    可以自定义每页多少数据,第几页等参数,这样前端把第几页、每页多少数据传给后台django应用,就可以获得相应的数据

    来自:https://www.django-rest-framework.org/api-guide/pagination/

    参考:

    https://docs.djangoproject.com/en/2.2/topics/pagination/

  • 相关阅读:
    docker 安装es
    Redis 和 Zookeeper 到底谁更牛?
    Redisson 看门狗
    记一次线上服务CPU 100%的处理过程
    必须了解的mysql三大日志-binlog、redo log和undo log
    python学习笔记 -- reduce合并减少
    Python学习笔记 -- 列表2: 遍历:嵌套列表, 将其中同位置的元素组成新的列表
    python学习笔记 -- filter() 过滤符合条件的可迭代序列
    python学习笔记 -- map() 操作可迭代序列
    python学习笔记
  • 原文地址:https://www.cnblogs.com/shengulong/p/10354323.html
Copyright © 2011-2022 走看看