AI代码解释 fromenumimportEnum from typingimportOptional,List # 自定义枚举类classModelName(Enum):boy="男"girl="女"unknown="不知道"@app.get("/item_enum")asyncdefread_item(name:str,sex:Optional[ModelName]=ModelName.unknown):
The workaround is quite verbose compared to just using a RootModel. No, the suppport for using Pydantic models as query parameters is not "great". Everything that you're hitting with RootModel in your original issue - FastAPI wanting to make it a Body rather than a Query, and not ...
指定枚举类型请求参数的栗子 from enum import Enum from typing import Optional, List # 自定义枚举类 class ModelName(Enum): boy = "男" girl = "女" unknown = "不知道" @app.get("/item_enum") async def read_item(name: str, sex: Optional[ModelName] = ModelName.unknown): return { "nam...
Python - FastAPI variable query parameters, If the query parameters are known when starting the API but you still wish to have them dynamically set: from fastapi import FastAPI, Depends from pydantic import create_model app = FastAPI () # Put your query arguments in this dict query_params = ...