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).

@vitalik vitalik self-assigned this Nov 14, 2025
@janrito
Copy link
Author

janrito commented Nov 18, 2025

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

yep – that is one of the implementations I used as reference. Listed above as https://github.com/kitware-resonant/django-ninja-cursor-pagination/tree/master/ninja_cursor_pagination

@janrito
Copy link
Author

janrito commented Nov 20, 2025

Is there anything else I can do to help, @vitalik ?

@vitalik
Copy link
Owner

vitalik commented Nov 25, 2025

@janrito looks like it sitll crashing

@janrito janrito force-pushed the feature/cursor-paginator branch from 98dd599 to 1fe184e Compare December 1, 2025 14:05
@janrito
Copy link
Author

janrito commented Dec 1, 2025

Hi @vitalik sorry for the delay, got a bit busy.

  1. I have fixed the mypy issues identified in the workflow
  2. I have rebased this changes to the main branch
  3. Unfortunately, this is leading to new mypy issues, but they are unrelated to the work in this branch:
    mypy ninja
    ninja/params/models.py:193: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:256: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:260: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:264: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:268: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:272: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:276: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:280: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/params/models.py:284: error: Unused "type: ignore" comment  [unused-ignore]
    ninja/testing/client.py:209:28: error: Item "StreamingHttpResponse" of "Response | StreamingHttpResponse" has no attribute "content"  [union-attr]
    Found 10 errors in 2 files (checked 49 source files)
    make: *** [lint] Error 1
  4. I am not having a great time testing on python3.7 - there is no package ready for arm macs. I have tried docker without much luck

@janrito janrito force-pushed the feature/cursor-paginator branch from 1fe184e to a1c008a Compare December 2, 2025 10:54
@janrito janrito force-pushed the feature/cursor-paginator branch from a1c008a to 5528fdf Compare December 5, 2025 16:43
@janrito
Copy link
Author

janrito commented Dec 5, 2025

Have rebased again from main.

test pass, but still getting these mypy errors that are unrelated to the work in this branch:

ruff format --preview --check ninja tests
150 files already formatted
ruff check --preview ninja tests
All checks passed!
mypy ninja
ninja/params/models.py:193: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:256: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:260: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:264: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:268: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:272: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:276: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:280: error: Unused "type: ignore" comment  [unused-ignore]
ninja/params/models.py:284: error: Unused "type: ignore" comment  [unused-ignore]
ninja/testing/client.py:209:28: error: Item "StreamingHttpResponse" of "Response | StreamingHttpResponse" has no attribute "content"  [union-attr]
Found 10 errors in 2 files (checked 49 source files)
make: *** [lint] Error 1

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