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:
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
resultsis returned andcountis still the total record count. - The upper bound of
page_sizeis controlled by theMAX_PAGE_SIZEclass attribute, which subclasses can override; the constructor also clampspage/page_sizeto 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.
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 ¶
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_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.
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 ¶
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 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.