Skip to content

Pagination

API Reference: Pagination

Pagination behavior is determined by the ViewSet's pagination_class, which defaults to PageNumberPagination. See Pagination for usage.

from srf.paginator import PageNumberPagination
from srf.views import BaseViewSet


class BookViewSet(BaseViewSet):
    queryset = Book.all()  # Tortoise QuerySet
    schema = BookSchema  # Pydantic model
    pagination_class = PageNumberPagination

Response structure

The object returned by list() always contains four fields:

{
  "count": 42,
  "next": true,
  "previous": false,
  "results": [{ "id": 1, "title": "..." }]
}

Differences from DRF

next / previous are booleans, not URLs to the next/previous page. The frontend must construct page numbers itself.

Query parameters

Parameter Default Rule
page 1 Falls back/clamps to 1 when it is not an integer or is less than 1
page_size 10 Falls back to 10 when it is not an integer, clamps to 1 when below 1, and is limited to MAX_PAGE_SIZE (default 100)
  • When the page number exceeds the total number of pages, an empty results is returned and count is still the total record count.
  • The upper bound of page_size is controlled by the MAX_PAGE_SIZE class attribute, which subclasses can override; the constructor also clamps page / page_size to valid ranges.
  • The parameter names are controlled by PAGE_QUERY_PARAM / PAGE_SIZE_QUERY_PARAM.

Paginator API

BasePagination

Base class for pagination styles (DRF-style).

Custom pagination must subclass this and implement the methods that raise NotImplementedError. Inheritance is the supported extension path.

from_queryset classmethod

from_queryset(queryset: QuerySet[T], request: Request) -> BasePagination

paginate async

paginate(sch_model: type[BaseModel] | None = None) -> PaginationResult

to_dict async

to_dict(sch_model: type[BaseModel] | None = None) -> dict[str, Any]

num_pages

num_pages(total_count: int) -> int

serialize_list

serialize_list(sch_model: type[BaseModel], rows: list[Any]) -> list[dict[str, Any]]

Convert ORM rows to JSON-ready dicts; override for custom list serialization.

options: heading_level: 4 members: - from_queryset - paginate - to_dict - num_pages - serialize_list show_source: false inherited_members: false

Custom paginators should inherit from BasePagination and implement from_queryset(), paginate(), and num_pages().

PageNumberPagination

PageNumberPagination(queryset: QuerySet[T], page: int = 1, page_size: int = 10)

Bases: BasePagination

A simple page number based style that supports page numbers as query parameters. For example:

/api/users/?page=1 /api/users/?page=2&page_size=100

Invalid or missing parameters fall back to page=1 / page_size=10.

Attributes:

Name Type Description
MAX_PAGE_SIZE int

Upper bound accepted for the page_size query parameter.

PAGE_QUERY_PARAM str

Name of the query parameter carrying the page number.

PAGE_SIZE_QUERY_PARAM str

Name of the query parameter carrying the page size.

queryset

The Tortoise QuerySet being paginated.

page

Current page number (1-based).

page_size

Items per page.

:param queryset: Tortoise ORM queryset :param page: 1-based page number; values below 1 are clamped. :param page_size: Items per page; clamped to 1..MAX_PAGE_SIZE.

MAX_PAGE_SIZE class-attribute instance-attribute

MAX_PAGE_SIZE: int = 100

PAGE_QUERY_PARAM class-attribute instance-attribute

PAGE_QUERY_PARAM: str = 'page'

PAGE_SIZE_QUERY_PARAM class-attribute instance-attribute

PAGE_SIZE_QUERY_PARAM: str = 'page_size'

queryset instance-attribute

queryset = queryset

page instance-attribute

page = max(int(page), 1)

page_size instance-attribute

page_size = min(max(int(page_size), 1), max(MAX_PAGE_SIZE, 1))

from_queryset classmethod

from_queryset(queryset: QuerySet[T], request: Request) -> PageNumberPagination

Parse page and page_size from request; fall back to defaults when invalid.

paginate async

paginate(sch_model: type[BaseModel] | None = None) -> PaginationResult

to_dict async

to_dict(sch_model: type[BaseModel] | None = None) -> dict[str, Any]

num_pages

num_pages(total_count: int) -> int

Return the total number of pages (empty results still have one page).

serialize_list

serialize_list(sch_model: type[BaseModel], rows: list[Any]) -> list[dict[str, Any]]

Convert ORM rows to JSON-ready dicts; override for custom list serialization.

options: heading_level: 4 members: - init - MAX_PAGE_SIZE - PAGE_QUERY_PARAM - PAGE_SIZE_QUERY_PARAM - from_queryset - paginate - to_dict - num_pages show_source: false inherited_members: false

PaginationResult

Bases: BaseModel

count instance-attribute

count: int

next instance-attribute

next: bool

previous instance-attribute

previous: bool

results instance-attribute

results: list[Any]

options: heading_level: 4 members: true show_source: false inherited_members: false

The fields of PaginationResult correspond to the four keys in the response.

serialize_page

serialize_page(schema: type[BaseModel], rows: list[Any]) -> list[dict[str, Any]]

Serialize ORM rows through a Reader/Writer schema for list responses.

Uses a cached TypeAdapter so schema structure is not re-built per row. Row data is still validated on every call; only the adapter is reused.

options: heading_level: 4 show_source: false

serialize_page() uses a TypeAdapter(list[schema]) cached per schema, making it suitable for reuse in custom paginators.