question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional pagination

See original GitHub issue

Is it possible to create a route that would return a paginated result if some params where passed, and a non-paginated result otherwise? It looks like add_pagination inspects the response_model, so I’m not sure how it can be done.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
uriyyocommented, Nov 27, 2022

Hi @davidbrochart @marioland ,

With version 0.11.0 you can achieve this using pagination_ctx function:

from typing import Union, List, Literal, Any

from fastapi import FastAPI, Depends, Query

from fastapi_pagination import Page, paginate, pagination_ctx

app = FastAPI()


@app.get(
    "/",
    response_model=Union[Page[int], List[int]],
    dependencies=[Depends(pagination_ctx(Page[int]))],
)
async def route(
    response_type: Literal["page", "list"] = Query(...),
) -> Any:
    data = [*range(100)]

    if response_type == "page":
        return paginate(data)

    return data


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app)
1reaction
uriyyocommented, Dec 7, 2022

Hi @davidbrochart, I am closing this issue, please reopen it if you have more questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional pagination with Spring - java - Stack Overflow
I am pretty new to java and spring. What I want to implement is api endpoint /tickets with pagination and sorting. I made...
Read more >
Best way to implement optional pagination and query strings ...
Best way to implement optional pagination and query strings on Laravel REST API. I have an App with 3 models: Customers, Orders and...
Read more >
Optional pagination for view-endpoints · Issue #273 - GitHub
I solved this issue by monkey patching PaginationMixin.paginate and _pagination_parameters_schema_factory . The force -Parameter of Blueprint.
Read more >
Is pagination optional? - Google Groups
I seem to remember some discussion around pagination being optional (maybe at the Cloud Identity Summit back in July?). However, in reading the...
Read more >
Pagination - Django REST framework
Pagination. Django provides a few classes that help you manage paginated data – that is, data that's split across several pages, with “Previous/Next”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found