0

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

martineau
  • 119,623
  • 25
  • 170
  • 301
Smaillns
  • 2,540
  • 1
  • 28
  • 40
  • 2
    Please [edit] to provide a [mcve], something that we can copy-paste to reproduce the problem. What does `return items` look like? What is the definition of `PydanticModel`? Do you mean `BaseModel`? The validation errors list out all the errors in the return value, so please [edit] those in as well. Something in the return value doesn't match the schema of `Page[PydanticModel]`, but it's not clear here what either looks like. – Gino Mempin Dec 29 '21 at 22:16

1 Answers1

0

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
Dhaval Jivani
  • 9,467
  • 2
  • 48
  • 42