Skip to main content

Zero-boilerplate pagination helpers for Django REST Framework

Project description

simple-drf-pagination

Zero-boilerplate pagination helpers for Django REST Framework.

This package provides a single factory function, paginate, that returns a DRF pagination class based on a short type string.

Installation

pip install simple-drf-pagination

Quick start

from simple_drf_pagination import paginate

# Page-number pagination
PagePagination = paginate(page_size=20, max_size=100)

# Limit/offset pagination
LimitPagination = paginate("limit", page_size=50, max_size=200)

# Cursor pagination
CursorPagination = paginate("cursor", page_size=25, ordering="-created_at")

Usage with DRF views

from rest_framework.viewsets import ModelViewSet
from simple_drf_pagination import paginate

class ItemViewSet(ModelViewSet):
    queryset = Item.objects.all()
    serializer_class = ItemSerializer
    pagination_class = paginate("page", page_size=20)

API

paginate(
    type: str = "page",
    page_size: int = 10,
    max_size: int = 100,
    ordering: str = "-id",
)

Parameters

  • type: "page", "limit", or "cursor". Defaults to "page".
  • page_size: Default page size for the pagination class.
  • max_size: Maximum page size or limit (page and limit types).
  • ordering: Cursor ordering field (cursor type only).

Behavior details

  • "page": returns a PageNumberPagination class with page_size, page_size_query_param="page_size", and max_page_size.
  • "limit": returns a LimitOffsetPagination class with default_limit and max_limit.
  • "cursor": returns a CursorPagination class with page_size and ordering.

Errors

If type is not one of "page", "limit", or "cursor", a ValueError is raised.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_drf_pagination-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_drf_pagination-0.1.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_drf_pagination-0.1.2.tar.gz.

File metadata

  • Download URL: simple_drf_pagination-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for simple_drf_pagination-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2d1f9cbbdc05c4a127a4bd2f7f014d87f7cbfa634911314bf1f78e018fedced9
MD5 a4cbad52f1cc3a73c9b910849131ed52
BLAKE2b-256 46aba27fefd6e0d43c4cd7b8f6dbe04d31c573817d95c96887b1329bc1325f77

See more details on using hashes here.

File details

Details for the file simple_drf_pagination-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_drf_pagination-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d73b76a51bc9d0fe9273ee9419326051935b54af16b3cf579e219dd22d1626c1
MD5 20f8fe19ef9ebfd4cefc4ac102a727e0
BLAKE2b-256 d4eba930dd3fce2da3fbf5826d3f0a0b6adfd5a8b12d50cce77c8fbdd5d1a08d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page