rest_framework.pagination

Pagination serializers determine the structure of the output that should be used for paginated responses.

Module Contents

_positive_int(integer_string, strict=False, cutoff=None)

Cast a string to a strictly positive integer.

_divide_with_ceil(a, b)

Returns ‘a’ divided by ‘b’, with any remainder rounded up.

_get_displayed_page_numbers(current, final)

This utility function determines a list of page numbers to display. This gives us a nice contextually relevant set of page numbers.

For example: current=14, final=16 -> [1, None, 13, 14, 15, 16]

This implementation gives one page to each side of the cursor, or two pages to the side when the cursor is at the edge, then ensures that any breaks between non-continuous page numbers never remove only a single page.

For an alternative implementation which gives two pages to each side of the cursor, eg. as in GitHub issue list pagination, see:

https://gist.github.com/tomchristie/321140cebb1c4a558b15

Given a list of page numbers and None page breaks, return a list of PageLink objects.

_reverse_ordering(ordering_tuple)

Given an order_by tuple such as (‘-created’, ‘uuid’) reverse the ordering and return a new tuple, eg. (‘created’, ‘-uuid’).

Cursor
PAGE_BREAK
class BasePagination

Bases:object

display_page_controls = False
paginate_queryset(self, queryset, request, view=None)
get_paginated_response(self, data)
to_html(self)
get_results(self, data)
get_schema_fields(self, view)
class PageNumberPagination

Bases:rest_framework.pagination.BasePagination

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

http://api.example.org/accounts/?page=4 http://api.example.org/accounts/?page=4&page_size=100

page_size
django_paginator_class
page_query_param = page
page_query_description
page_size_query_param
page_size_query_description
max_page_size
last_page_strings = ['last']
template = rest_framework/pagination/numbers.html
invalid_page_message
paginate_queryset(self, queryset, request, view=None)

Paginate a queryset if required, either returning a page object, or None if pagination is not configured for this view.

get_paginated_response(self, data)
get_page_size(self, request)
get_html_context(self)
to_html(self)
get_schema_fields(self, view)
class LimitOffsetPagination

Bases:rest_framework.pagination.BasePagination

A limit/offset based style. For example:

http://api.example.org/accounts/?limit=100 http://api.example.org/accounts/?offset=400&limit=100

default_limit
limit_query_param = limit
limit_query_description
offset_query_param = offset
offset_query_description
max_limit
template = rest_framework/pagination/numbers.html
paginate_queryset(self, queryset, request, view=None)
get_paginated_response(self, data)
get_limit(self, request)
get_offset(self, request)
get_html_context(self)
to_html(self)
get_schema_fields(self, view)
get_count(self, queryset)

Determine an object count, supporting either querysets or regular lists.

class CursorPagination

Bases:rest_framework.pagination.BasePagination

The cursor pagination implementation is necessarily complex. For an overview of the position/offset style we use, see this post: https://cra.mr/2011/03/08/building-cursors-for-the-disqus-api

cursor_query_param = cursor
cursor_query_description
page_size
invalid_cursor_message
ordering = -created
template = rest_framework/pagination/previous_and_next.html
page_size_query_param
page_size_query_description
max_page_size
offset_cutoff = 1000
paginate_queryset(self, queryset, request, view=None)
get_page_size(self, request)
get_ordering(self, request, queryset, view)

Return a tuple of strings, that may be used in an order_by method.

decode_cursor(self, request)

Given a request with a cursor, return a Cursor instance.

encode_cursor(self, cursor)

Given a Cursor instance, return an url with encoded cursor.

_get_position_from_instance(self, instance, ordering)
get_paginated_response(self, data)
get_html_context(self)
to_html(self)
get_schema_fields(self, view)