[英]return deeply nested json objects with response_model fastAPI and Pydantic
这是我的架构文件
from pydantic import BaseModel
from typing import Optional
class HolidaySchema(BaseModel):
year: int
month: int
country: str
language: str
class HolidayDateSchema(BaseModel):
name: str
date: str
holidays: HolidaySchema | None = None
class Config:
orm_mode = True
这是我的路由器
@router.get("/holidays/",response_model = List[HolidayDateSchema])
我想得到的回应是
[
{
"date": "2021-08-14",
"name": "Independence Day",
"holidays": { "year": 2022, "month":5, "country":"pk", "language":"en"},
"id": 13
},
]
现在它不支持响应 model 的 pydantic 模式,我不知道为什么,它给出了错误pydantic.error_wrappers.ValidationError: 2 validation errors for HolidayDateSchema
并且value is not a valid dict
如果任何人都可以使用 response_model 指定获得深度嵌套的 JSON 对象的最佳方法,那就太好了。
HolidaySchema 未配置orm_mode = True
。
对于要从 SQLAlchemy model 对象自动转换的所有模型,您都需要这个。
class HolidaySchema(BaseModel):
year: int
month: int
country: str
language: str
class Config:
orm_mode = True
如果您想要所有模型的设置,您可以在通用 BaseModel 上配置该设置并从该设置继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.