Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pagination using negative page_number or offset #9568

Conversation

dennybiasiolli
Copy link
Contributor

@dennybiasiolli dennybiasiolli commented Oct 19, 2024

Description

What do you thing about allowing users to paginate using a negative page_number or offset in pagination?

It could be useful when API users want to get the last x results in a queryset without having to make a first API call to get the total number or results/pages.

I took inspiration from the Python negative indexing in lists.

I know that for the last page we can use PageNumberPagination with page=last as a query parameter (or customizing the last_page_strings value), but what about the LimitOffsetPagination?

Let's say we want to go to the second (or third) last page using PageNumberPagination in the first API call,
or get the last 10 records using LimitOffsetPagination with a single API call, at the moment I don't think it possible.

We would have to:

  • call the API once to get the count of the results (i.e. 100, it's the first API call and we didn't know the count yet)
  • call the API a second time with limit=10 and offset=90

After this PR we can do a single API call with limit=10 and offset=-10.

Of course I set this as optional via the allow_negative_page_numbers for PageNumberPagination and via allow_negative_offsets for LimitOffsetPagination

@ulgens
Copy link

ulgens commented Oct 20, 2024

@dennybiasiolli Asking out of curiosity, is there any other REST API frameworks/libraries doing this? Normally, if you have the object count or page count, a client can simply "jump" to the last page by doing simple math.

@dennybiasiolli
Copy link
Contributor Author

@ulgens I took inspiration from the Python negative indexing in lists.

I know that for the last page we can use PageNumberPagination with page=last as a query parameter (or customizing the last_page_strings value), but what about the LimitOffsetPagination?

Let's say we want to go to the second (or third) last page using PageNumberPagination in the first API call,
or get the last 10 records using LimitOffsetPagination with a single API call, at the moment I don't think it possible.

We would have to:

  • call the API once to get the count of the results (i.e. 100, it's the first API call and we didn't know the count yet)
  • call the API a second time with limit=10 and offset=90

After this PR we can do a single API call with limit=10 and offset=-10.

Of course I set this as optional via the allow_negative_page_numbers for PageNumberPagination and via allow_negative_offsets for LimitOffsetPagination

@ulgens
Copy link

ulgens commented Oct 20, 2024

Let's say we want to go to the second (or third) last page using PageNumberPagination in the first API call,
or get the last 10 records using LimitOffsetPagination with a single API call, at the moment I don't think it possible.

Sorry, but I can't imagine a common valid use case for "third last page" access. I'm not saying the use case is not valid, but I can't see it as common enough to be included in DRF, especially considering DRF is not accepting any new features right now.

Can you please share if any of the tools you know has this feature? Thanks in advance.

@dennybiasiolli
Copy link
Contributor Author

Can you please share if any of the tools you know has this feature?

I'm using django-rest-framework and of course it doesn't have this feature, that's the reason for my PR.

I didn't know DRF was not accepting new features, you can just close and reject this PR then.

@ulgens
Copy link

ulgens commented Oct 20, 2024

(I don't have any maintainer permissions on the repo, just tried to share what I know / understand 🌻 )

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

page number with negetive page number? didn't know this was a thing! can't you just use a custom paginator for your rojects specific need?

@dennybiasiolli
Copy link
Contributor Author

@auvipy yes, of course, we implemented our custom paginator handling negative offset, then I thought someone else might benefit from that and I tried to make this PR.

Our focus was more on the negative offset because we needed last x results.
Then I implemented the same in negative page numbers passing by.

@tomchristie
Copy link
Member

I didn't know DRF was not accepting new features, you can just close and reject this PR then.

Thanks @dennybiasiolli yep apologies that our messaging isn't clear enough on that yet. Nice piece of work & appreciate the interest/suggestion. 🙏🏼

Related... anyone want to take this as a prompt to PR up our messaging here?

@dennybiasiolli dennybiasiolli deleted the allowing-negative-pagination branch October 21, 2024 16:07
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.

4 participants