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.

Paginate inner model field

See original GitHub issue

Great library, thanks!

How would you go about using it on an endpoint which doesn’t directly return a sequence, but a model in which a field is the sequence to paginate, e.g.:

class ResponseType(BaseModel):
    other_data: int
    data_to_paginate: Sequence[str]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
uriyyocommented, Dec 7, 2022

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

1reaction
uriyyocommented, Nov 27, 2022

Hi @davidbrochart,

Now it should be easier to implement this feature. Example:

from typing import Any, TypeVar, Generic, Sequence, Type

from fastapi import FastAPI
from pydantic import BaseModel, Field

from fastapi_pagination import paginate, Params
from fastapi_pagination.api import pagination_items, add_pagination
from fastapi_pagination.bases import AbstractPage, AbstractParams

app = FastAPI()
add_pagination(app)

T = TypeVar("T")
C = TypeVar("C")


class PageWithInnerItems(AbstractPage[T], Generic[T]):
    __root__: T
    __params_type__ = Params

    @classmethod
    def create(
            cls: Type[C],
            items: Sequence[T],
            params: AbstractParams,
            **kwargs: Any,
    ) -> C:
        return cls(__root__=kwargs)


class ResponseType(BaseModel):
    other_data: int
    data_to_paginate: Sequence[str] = Field(default_factory=pagination_items)


@app.get(
    "/",
    response_model=PageWithInnerItems[ResponseType],
)
async def route() -> Any:
    data = [*range(100)]

    return paginate(data, additional_data={"other_data": 1000000})


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Pagination. Lately, I faced a problem while working…
Find a way to paginate books inside the author instance. The first solution is easy, and won't actually take five minutes to implement, ......
Read more >
Django how to sort and paginate nested ORM and non-ORM ...
all works fine but there is a need to order data by some fields of the Property model, order by total price and...
Read more >
Paginate related fields - Google Groups
However, we have a need to paginate relationship fields that return multiple items ... ModelSerializer): class Meta: model = Track fields = ('order', ......
Read more >
Example of doing pagination (especially on nested resources ...
I'm working with a graphQL API that has a top level resource that has several fields in it that are paginated themselves -...
Read more >
Best practices for consuming nested pagination query results?
Hi all,. I'm fairly new to GraphQL in general, and I'm looking for some guidance on how to paginate nested query results.
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