【发布时间】:2020-07-07 06:47:38
【问题描述】:
我希望在 FastApi 上有一个动态强制主体。
我解释一下:
from fastapi import FastAPI, Body
from pydantic import BaseModel
app = FastAPI()
class Parameters(BaseModel):
platform: str
country: str
@app.put("/myroute")
async def provision_instance(*, parameters: Parameters = Body(...)):
do_something
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=80)
这里,我的body是在Parameters类中手动定义的,有两个属性,平台和国家。 未来,这些属性将来自一个配置文件,并且会有两个以上的属性。所以我需要动态地自动创建它们。
例如,在配置文件中,我可以有:
---
parameters:
application:
description: "Name of the application"
type: string
platform:
description: "Name of the platform"
type: string
country:
description: "Name of the country"
type: string
在这种情况下,我如何在正文中拥有可变数量的参数?我应该找到一种方法为我的参数类提供可变数量的属性吗?
【问题讨论】: