获取多个项目,根据任意查询 -> Python FastAPI

huangapple go评论121阅读模式
英文:

Get multiple items given an arbitrary query -> Python FastAPi

问题

获取多个项目的任意查询。我正在尝试使用Python Fastapi实现这一目标,这是我在路由中所做的事情:

  1. def get_props_query(
  2. *,
  3. session: Session = Depends(get_session),
  4. query: Optional[Props] = Query(
  5. default=select(PropsTable), description="它是可选的"
  6. )):

但是出现以下错误:

  1. raise fastapi.exceptions.FastAPIError(
  2. fastapi.exceptions.FastAPIError: 响应字段的参数无效提示检查 typing.Optional

创建了这样的模型:

  1. from pydantic import BaseModel
  2. class Props(BaseModel):
  3. query: Optional[str]
英文:

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

  1. def get_props_query(
  2. *,
  3. session: Session = Depends(get_session),
  4. query: Optional[Props] = Query(
  5. default=select(PropsTable), description="It is optional"
  6. )):

but getting this error ->

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

Created a model like this

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

答案1

得分: 0

这部分内容的翻译如下:

"这个部分的翻译如下:

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

  1. def get_props_query(
  2. *,
  3. session: Session = Depends(get_session),
  4. query: Union[str, None] = Query(default=None, description="It is optional"),
  5. ):
英文:

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

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

huangapple
  • 本文由 发表于 2023年1月9日 16:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定