简体   繁体   English

我应该如何在 Optional[] 中定义创建子可选模型,使用 FastAPI python 的输入和 pydantic 库?

[英]How shall i define create sub-optional models in Optional[] working with typing and pydantic libs for FastAPI python?

Hi i'm a beginer in FastAPi, getting this error as嗨,我是 FastAPi 的初学者,收到此错误

TypeError: typing.Union[pydantic.main.stats, NoneType] is not a generic class.类型错误:typing.Union[pydantic.main.stats, NoneType] 不是泛型类。

How shall i create sub-optional models?我应该如何创建子可选模型? these are my imports.这些是我的进口。

from typing import Optional,List
from pydantic import BaseModel, create_model

在此处输入图片说明 在此处输入图片说明

You can just use an optional nested model, as described in the doc您可以只使用可选的嵌套模型,如文档中所述

from typing import Optional, Set

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []
    image: Optional[Image] = None


# do your stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM