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/

  • 相关阅读:
    LINUX创建文件和目录的默认权限
    Fiddler工具使用介绍一
    Jmeter使用指南
    LINUX中如何查看某个端口是否被占用
    Linux下重要日志文件及查看方式
    Linux如何查看文件的创建、修改时间?
    Linux 系统日志和系统信息常用命令介绍
    linux查看系统的日志的一些实用操作
    Linux下查看/管理当前登录用户及用户操作历史记录
    4*4(齐次)矩阵
  • 原文地址:https://www.cnblogs.com/shengulong/p/10354323.html
Copyright © 2011-2022 走看看