如何改变Pydantic的行为如何包括一个额外的深层领域
我有两个后续课程。
class Library(BaseModel):
package: str
class JobTaskSettings(TaskKey):
description: Optional[str]
depends_on: Optional[List[TaskKey]]
on_cluster_id: str = Field(..., alias="existing_cluster_id")
libraries: List[Library]
当我调用.dict
doc时, 在jobtasksettings
上,我想
{
"libraries": [
{
"pypi": {
"package": "requests"
}
},
{
"pypi": {
"package": "bs4"
}
}
]
}
在每个库模型中都用“ PYPI”键包裹在另一个JSON对象的情况下,以下结果。 这可能吗?
I have the two followings Class
class Library(BaseModel):
package: str
class JobTaskSettings(TaskKey):
description: Optional[str]
depends_on: Optional[List[TaskKey]]
on_cluster_id: str = Field(..., alias="existing_cluster_id")
libraries: List[Library]
when I call .dict
Doc on JobTaskSettings
, I would like to have the following result
{
"libraries": [
{
"pypi": {
"package": "requests"
}
},
{
"pypi": {
"package": "bs4"
}
}
]
}
Where to each Library Model is wrapped around another json Object with the "pypi" key.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要添加另一个类和Modifi库类:
和输出:
这比更改.dict()或.json()Bechaviour更安全
All you need is only add one more class and modifi Library class:
and the output:
This is more safe than changing .dict() or .json() bechaviour