gpt4 book ai didi

python - FastAPI - 未填充 ENUM 类型模型

转载 作者:行者123 更新时间:2023-12-03 23:59:22 28 4
gpt4 key购买 nike

下面是我的 fastAPI 代码

from typing import Optional, Set

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl, Field
from enum import Enum

app = FastAPI()


class Status(Enum):
RECEIVED = 'RECEIVED'
CREATED = 'CREATED'
CREATE_ERROR = 'CREATE_ERROR'


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


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
results = {"item_id": item_id, "item": item}
return results
下面是生成的 swagger 文档。状态未显示。我是 pydantic 的新手,我不确定如何在文档中显示状态
enter image description here

最佳答案

创建 Status 类通过从 继承str Enum

class Status(str, Enum):
RECEIVED = 'RECEIVED'
CREATED = 'CREATED'
CREATE_ERROR = 'CREATE_ERROR'
引用文献
  • Working with Python enumerations--(FastAPI doc)
  • [BUG] docs don't show nested enum attribute for body--(Issue #329)
  • 关于python - FastAPI - 未填充 ENUM 类型模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64160594/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com