Snippets: Python: DRF: Custom Pagination

 20th August 2020 at 2:19pm

自定义分页功能。

下面定义了 page_size 作为 API 中指定每页大小的查询参数:

# <your_project>/<your_project>/pagination.py
from rest_framework import pagination


class CustomPagination(pagination.PageNumberPagination):
    page_size_query_param = "page_size"
    max_page_size = 100

settings.py 中加入:

REST_FRAMEWORK = {
    # ...
    'DEFAULT_PAGINATION_CLASS': '<your_project>.pagination.CustomPagination',
}