Get multiple items given an arbitrary query -> Python FastAPi

131 Views Asked by At

Get multiple items given an arbitrary query. I am trying to achieve this using Python Fastapi, this is what I did in routes,

def get_props_query(

    *,
    session: Session = Depends(get_session),
    query: Optional[Props] = Query(
            default=select(PropsTable), description="It is optional"
        )):

but getting this error ->

raise fastapi.exceptions.FastAPIError(
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that typing.Optional

Created a model like this

from pydantic import BaseModel
class Props(BaseModel):
    query: Optional[str]
1

There are 1 best solutions below

0
On BEST ANSWER

Well this worked fine, I wanted to pass RAW SQL query from FastAPI swagger UI. This is what I done in the routes

def get_props_query(
    *,
    session: Session = Depends(get_session),
    query: Union[str, None] = Query(default=None, description="It is optional"),
):