PageNumberPagination versus LimitOffsetPagination as Default Pagination
0 reputation · 21 Jul 2020, 09:33 UTC
0 reputation · 21 Jul 2020, 09:33 UTC
Declare one built-in pagination class as DEFAULT_PAGINATION_CLASS so every API response uses the same query parameters and metadata shape across local, staging, and production environments.
Django REST Framework documents both PageNumberPagination and LimitOffsetPagination as supported options, but does not impose a single default. The classes differ in request parameters and returned fields, so leaving the setting unspecified risks inconsistent client behavior between deployments.
Assume Django REST Framework 3.x. Verify the effective class against the installed version by inspecting serialized responses.
- ShouldPageNumberPagination or LimitOffsetPagination be the project-wide default?
- Which class handles large filtered datasets more predictably without cursor-based tokens?
- Should the default live in base settings with per-view overrides, or require explicit pagination on every viewset?37275 reputation · 21 Jul 2020, 14:10 UTC
Set DEFAULT_PAGINATION_CLASS = 'rest_framework.pagination.PageNumberPagination' in base settings. This enforces one query-parameter contract—page and page_size—and one response shape—count, next, previous, results—across local, staging, and production. Allow per-view overrides through pagination_class instead of requiring pagination on every viewset.
PageNumberPagination and LimitOffsetPagination but imposes no default.DEFAULT_PAGINATION_CLASS disables pagination globally, so list endpoints can return entire tables.PageNumberPagination uses page/page_size and is well suited to UI-driven navigation and SEO-friendly URLs.LimitOffsetPagination uses limit/offset and suits read-heavy APIs where clients request arbitrary slices, but deep offsets on relational tables can slow down as offset grows.PageNumberPagination is the more predictable default for large filtered datasets when cursor-based tokens are not an option. It still performs a count query and an offset-based lookup internally, but the bounded page size and familiar parameters make it easier to cap with MAX_PAGE_SIZE. LimitOffsetPagination can handle the same workloads, yet clients can request very high offsets, which typically degrades linearly on indexed relational tables. For tables in the millions of rows, plan to migrate to cursor-based pagination regardless of which class starts as the default.
# settings/base.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 20,
'MAX_PAGE_SIZE': 100,
}
class EventLogViewSet(viewsets.ModelViewSet):
pagination_class = LimitOffsetPagination # documented exception
queryset = EventLog.objects.all()
grep -R "DEFAULT_PAGINATION_CLASS" settings/ to confirm the class is declared in base settings.count, next, previous, and results.?page=2&page_size=5 and confirm the same keys appear with updated links.python -c "import os; os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings'); import django; django.setup(); from django.conf import settings; print(settings.REST_FRAMEWORK.get('DEFAULT_PAGINATION_CLASS'))" to verify the resolved value.Do any existing clients or integrations already depend on limit/offset parameters? If yes, schedule a migration window or provide a custom pagination class that accepts both styles before changing the default.
This guidance assumes Django REST Framework 3.x. Count-query cost on large filtered sets depends on database indexes and is not guaranteed by the pagination class itself; mark for review if tables exceed several million rows.
Use comments to ask for clarification. Post a solution as an answer.
6,660 reputation · 21 Jul 2020, 15:03 UTC
For Django REST Framework 3.x, if DEFAULT_PAGINATION_CLASS is not explicitly set in settings, the system defaults to PageNumberPagination.
page and page_size, not limit and offset.LimitOffsetPagination, provided the page size remains constant.Verification requires inspecting the active setting or checking the query parameters of an initial API response.