Skip to content

Pagination classes

Async-aware paginators that drive the apaginate_queryset() hook on async views. See the Pagination guide for selection criteria and tuning.

BasePagination

Bases: BasePagination

All pagination classes should extend BasePagination. Adds an async apaginate_queryset hook that defaults to running the sync paginate_queryset in a thread.

apaginate_queryset async

apaginate_queryset(
    queryset: QuerySet[Any, Any],
    request: Request,
    view: APIView | None = None,
) -> list[Any] | None

Returns a list of items for a single page of the queryset, or None if pagination is disabled.

PageNumberPagination

Bases: BasePagination, PageNumberPagination

A simple page number based style that supports page numbers as query parameters. Adds an async apaginate_queryset that uses async ORM (acount, async iteration) instead of sync_to_async.

apaginate_queryset async

apaginate_queryset(
    queryset: QuerySet[Any, Any],
    request: Request,
    view: APIView | None = None,
) -> list[Any] | None

Returns a list of items for the requested page, or None if pagination is disabled.

LimitOffsetPagination

Bases: BasePagination, LimitOffsetPagination

A limit/offset based style. Adds an async apaginate_queryset that uses async ORM (acount, async iteration) instead of sync_to_async.

apaginate_queryset async

apaginate_queryset(
    queryset: QuerySet[Any, Any],
    request: Request,
    view: APIView | None = None,
) -> list[Any] | None

Returns a list of items for the current limit/offset window, or None if pagination is disabled.

CursorPagination

Bases: BasePagination, CursorPagination

The cursor pagination implementation is necessarily complex. Adds an async apaginate_queryset that mirrors DRF's paginate_queryset using async ORM iteration instead of sync_to_async.

apaginate_queryset async

apaginate_queryset(
    queryset: QuerySet[Any, Any],
    request: Request,
    view: APIView | None = None,
) -> list[Any] | None

Returns a list of items for the current cursor window, or None if pagination is disabled.

FastPageNumberPagination

Bases: BasePagination

Page number pagination that skips the COUNT(*) query. Determines whether a next page exists by checking if the current page is full, and omits the count field from the response.

get_page_size

get_page_size(request: Request) -> int | None

Returns the page size for the current request.

get_page_number

get_page_number(request: Request) -> int

Returns the requested page number from the query string.

paginate_queryset

paginate_queryset(
    queryset: QuerySet[Any, Any] | Sequence[Any],
    request: Request,
    view: APIView | None = None,
) -> list[Any] | None

Returns a list of items for the requested page without issuing a COUNT(*).

apaginate_queryset async

apaginate_queryset(
    queryset: QuerySet[Any, Any],
    request: Request,
    view: APIView | None = None,
) -> list[Any] | None

Async variant of paginate_queryset using async ORM iteration.

get_paginated_response

get_paginated_response(data: Any) -> Response

Returns the paginated response with results, next, and previous links.