Here is my code snippet:
from fastapi_pagination import Page, add_pagination
from fastapi_pagination.ext.sqlalchemy import paginate
@app.get("/clients", response_model=Page[PydanticModel])
def get_items(
db: Session = Depends(get_db) ) -> Any:
items = paginate(
db.query(Model)
.filter(...)
)
...
# do some extra manipulations ..
...
items.items = new_items
return items
When I specify Page[PydenticModel]
in the response_model
it generates an issue with paginte()
because it's not the final response type. The PydenticModel
correspond to new_items
and not items
(returned from paginate()
),
pydantic.error_wrappers.ValidationError: validation errors for
Page[PydanticModel]
Note: I don't want to use Page[Any] in order to keep a good a Swagger docs
I think you need PydenticModel to have orm_mode = True in its config
I am facing the same issue while using pagination in fast API and resolving the issue by adding orm_mode = True in the model class.
Search for orm_mode here for more detail
Sample class with orm_mode = True
class Todo(BaseModel):
id:int
title: str
description: Optional[str]
priority: int
complete: bool
owner_id:int
class Config:
orm_mode = True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.