Skip to main content

AsyncPaginator

This class provides asynchronous pagination capabilities for object lists, extending the base pagination logic with coroutine-based methods. It supports asynchronous iteration over pages and provides awaitable methods for counting items, validating page numbers, and retrieving specific page objects. The class is designed to work with both standard sequences and objects that implement asynchronous count methods.

Attributes

AttributeTypeDescription
_cache_acountinteger = NoneInternal cache for the total number of objects in the object_list to avoid redundant asynchronous count operations.
_cache_anum_pagesinteger = NoneInternal cache for the total number of pages calculated based on the object count and pagination settings.

Constructor

Signature

def AsyncPaginator(
object_list: Any,
per_page: int,
orphans: int = 0,
allow_empty_first_page: bool = True,
error_messages: dict = None
) - > null

Parameters

NameTypeDescription
object_listAnyA list, tuple, or other sequence-like object to be paginated.
per_pageintThe maximum number of items to include on a single page.
orphansint = 0The minimum number of items allowed on the last page.
allow_empty_first_pagebool = TrueWhether the first page is allowed to be empty.
error_messagesdict = NoneOptional dictionary of custom error messages.

Methods


avalidate_number()

@classmethod
def avalidate_number(
number: int|str
) - > int

Validates the given page number against the total number of pages in the result set.

Parameters

NameTypeDescription
number`intstr`

Returns

TypeDescription
intThe validated page number as an integer

aget_page()

@classmethod
def aget_page(
number: int|str
) - > [AsyncPage](asyncpage.md?sid=django_core_paginator_asyncpage)

See Paginator.get_page().

Parameters

NameTypeDescription
number`intstr`

Returns

TypeDescription
[AsyncPage](asyncpage.md?sid=django_core_paginator_asyncpage)The requested page, or the first page if the input is invalid, or the last page if the input is out of range

apage()

@classmethod
def apage(
number: int|str
) - > [AsyncPage](asyncpage.md?sid=django_core_paginator_asyncpage)

See Paginator.page().

Parameters

NameTypeDescription
number`intstr`

Returns

TypeDescription
[AsyncPage](asyncpage.md?sid=django_core_paginator_asyncpage)An AsyncPage object containing the slice of objects for the specified page number

acount()

@classmethod
def acount() - > int

See Paginator.count().

Returns

TypeDescription
intThe total number of objects across all pages, cached after the first call

anum_pages()

@classmethod
def anum_pages() - > int

See Paginator.num_pages().

Returns

TypeDescription
intThe total number of pages calculated based on the count and items per page

apage_range()

@classmethod
def apage_range() - > range

See Paginator.page_range()

Returns

TypeDescription
rangeA 1-based range object representing the sequence of available page numbers

aget_elided_page_range()

@classmethod
def aget_elided_page_range(
number: int = 1,
on_each_side: int = 3,
on_ends: int = 2
) - > AsyncIterator[int|str]

Generates a page range with elided sections (ellipses) for use in large pagination sets.

Parameters

NameTypeDescription
numberint = 1The current active page number around which to center the range
on_each_sideint = 3The number of pages to include on each side of the current page
on_endsint = 2The number of pages to include at the beginning and end of the range

Returns

TypeDescription
`AsyncIterator[intstr]`