所以我有这个类(class):
class Table(BaseModel):
__root__: Dict[int, Test]
我使用的是 __root__
因为它是一个动态值,但是当我转到 /redoc
(我使用的是 FastAPI)时,示例值默认为如下图所示为 property1
、property2
,但我希望默认示例为 id_1
、id_2
。我怎样才能实现这个目标?
我尝试使用库的 Field 方法更改别名和标题,但似乎没有像我预期的那样工作。
最佳答案
您是否尝试过使用 schema customization 来做到这一点? ?
class QcastTable(BaseModel):
__root__: Dict[int, Test]
class Config:
schema_extra = {
"examples": [
{
"id_1": {
"title": "yourTitle",
"description": "yourDescription",
},
"id_2": {
"title": "yourTitle2",
"description": "yourDescription2",
}
}
]
}
关于python-3.x - 如何更改 Pydantic 默认 __root__ 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74319416/