-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Allow pagination using negative page_number
or offset
#9568
Conversation
@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. |
@ulgens I took inspiration from the Python negative indexing in lists. I know that for the last page we can use Let's say we want to go to the second (or third) last page using We would have to:
After this PR we can do a single API call with Of course I set this as optional via the |
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. |
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. |
(I don't have any maintainer permissions on the repo, just tried to share what I know / understand 🌻 ) |
There was a problem hiding this 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?
@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. |
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? |
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
withpage=last
as a query parameter (or customizing thelast_page_strings
value), but what about theLimitOffsetPagination
?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:
limit=10
andoffset=90
After this PR we can do a single API call with
limit=10
andoffset=-10
.Of course I set this as optional via the
allow_negative_page_numbers
forPageNumberPagination
and viaallow_negative_offsets
forLimitOffsetPagination