1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Pydantic validation issue when using pagination with FastApi

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024 às 22:32.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página