I have an enum class for storing some categorical values like this.
class Fields(str, Enum):
text = "text"
para = "para"
images = "images"
And there are pydantic models for each of these types. For example:
class imageModel(BaseModel):
min_width : int
max_height : int
is_exact : int
is_proportional : int
default_mode : int
default_quality : int
And I have a dict like this:
type_attrs = {
"text": textModel,
"para": ParaModel,
"image": imageModel
}
I have a FastAPI route where the user needs to input the Field type name as string (taken as dropdown from fastapi docs) and supply the type attributes according to the type chosen.
If the user selects type = "images", the corresponding pydantic model "ImageModel" would be provided for thr user to fill in and so on.
Is there any way the corresponding pydantic model can be produced after selecting the type name?
Thanks.
question from:
https://stackoverflow.com/questions/65833973/is-there-a-way-to-generate-request-models-for-the-selected-request-params-in-fas 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…