Wrap paginated data #1375
Answered
by
uriyyo
kamilos956
asked this question in
Q&A
Wrap paginated data
#1375
-
Hello, I have a question about wrapping a pagination data. I have an example:
But I would like to have:
Is it a simple way to do this? I've created a custom class, where I inherited AbstractPage and write custom create classmethod, and return custom BaseModel object and achieve what I want. But I don't like this solution, because I lost all calculations of parameters (i.e. pages) and some checks, which are done under the hood of Page class. |
Beta Was this translation helpful? Give feedback.
Answered by
uriyyo
Dec 2, 2024
Replies: 1 comment
-
Hi @kamilos956, I guess the esiest way will be smth like this: from typing import TypeVar, Any
from pydantic import computed_field
from fastapi_pagination import Page
T = TypeVar("T")
class CustomPage(Page[T]):
__model_exclude__ = {
"total",
"page",
"size",
"pages",
}
@computed_field
def pagination(self) -> dict[str, Any]:
return {
"total": self.total,
"page": self.page,
"size": self.size,
"pages": self.pages,
} from fastapi_pagination import paginate, set_page, set_params, Params
set_page(CustomPage[int])
set_params(Params(page=2, size=2))
page = paginate(range(200))
print(page.model_dump_json(indent=2)) {
"items": [
2,
3
],
"pagination": {
"total": 200,
"page": 2,
"size": 2,
"pages": 100
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
kamilos956
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @kamilos956,
I guess the esiest way will be smth like this: