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

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

Get multiple items given an arbitrary query -> Python FastAPi

问题

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

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

但是出现以下错误:

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

创建了这样的模型:

from pydantic import BaseModel
class Props(BaseModel):
    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,

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

得分: 0

这部分内容的翻译如下:

"这个部分的翻译如下:

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"),
):
英文:

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"),
):

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:

确定