is a valid pydantic field type"-6ren"> is a valid pydantic field type"-我有以下用于 FastAPI 应用的 Pydantic 架构。 在下面的模式中,每当我将 ParameterSchema 作为 params 的模式验证器时,它会给我以下错误: fastapi.exc-6ren">
gpt4 book ai didi

python - "FastAPIError: Invalid args for response field! Hint: check that is a valid pydantic field type"

转载 作者:行者123 更新时间:2023-12-05 03:39:58 25 4
gpt4 key购买 nike

我有以下用于 FastAPI 应用的 Pydantic 架构。

在下面的模式中,每当我将 ParameterSchema 作为 params 的模式验证器时,它会给我以下错误:

fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'typing._GenericAlias'> is a valid pydantic field type

我不知道发生了什么!

class ParameterSchema(BaseModel):
expiryDate = Optional[datetime]

class Config:
arbitrary_types_allowed = True


class RequestProvisioningEventData(BaseModel):
some_attribute: List[str]
other_attribute: Optional[List[str]] = []
bool_attribute: bool
params: ParameterSchema

class Config:
use_enum_values = True

最佳答案

这是因为 expiryDate分配 (=) 一个值

class ParameterSchema(BaseModel):
expiryDate = Optional[datetime]

它应该使用 type hint (:):

class ParameterSchema(BaseModel):
expiryDate: Optional[datetime]

注意使用冒号:作为类型提示,不等于=

关于python - "FastAPIError: Invalid args for response field! Hint: check that <class ' 打字._UnionGenericAlias '> is a valid pydantic field type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68336259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com