Skip to content

Conversation

@janrito
Copy link

@janrito janrito commented Nov 4, 2025

Hi

I have implemented a version of CursonPaginator based on ideas from both:
https://github.com/kitware-resonant/django-ninja-cursor-pagination/tree/master/ninja_cursor_pagination
and https://github.com/encode/django-rest-framework/blob/2465f9b4876ce16f2d22aa8d9f3cd20abde1b7c8/rest_framework/pagination.py#L583

This is an attempt for a minimal implementation that serves my needs, happy to hear any feedback.

It defines a custom paginator which can be added to any call:

from ninja.pagination import paginate, CursorPagination

@api.get('/events', response=List[EventSchema])
@paginate(CursorPagination)
def list_events(request):
    return Event.objects.all()

It defaults to using -created as the positioning attribute and allows for additional ordering attributes. These, along a page_size and max_page_size can be customised per view:

@api.get("/events")
@paginate(CursorPagination, ordering=("start_date", "end_date"), page_size=20, max_page_size=100)
def list_events(request):
    return Event.objects.all()

Where start_date is the positioning attribute

@janrito janrito marked this pull request as draft November 4, 2025 17:03
@janrito janrito force-pushed the feature/cursor-paginator branch from 1eecf86 to d12575e Compare November 4, 2025 17:18
@janrito janrito marked this pull request as ready for review November 4, 2025 17:25
@vitalik
Copy link
Owner

vitalik commented Nov 6, 2025

Hi @janrito

I think thi default is not practicaly - not many will have 'created' - maybe default should be '-pk' ?

PAGINATION_DEFAULT_ORDERING: tuple[str, ...] = Field(
        ("-created",), alias="NINJA_PAGINATION_DEFAULT_ORDERING"
    )

@janrito
Copy link
Author

janrito commented Nov 6, 2025

Hi @janrito

I think thi default is not practicaly - not many will have 'created' - maybe default should be '-pk' ?

PAGINATION_DEFAULT_ORDERING: tuple[str, ...] = Field(
        ("-created",), alias="NINJA_PAGINATION_DEFAULT_ORDERING"
    )

Makes sense, have updated here: 1bddcaa

@janrito janrito force-pushed the feature/cursor-paginator branch from ce7e7c1 to 98dd599 Compare November 6, 2025 11:06
@danlamanna
Copy link

FWIW - this is related to #836 (FYI @zachmullen).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants