from fastapi import FastAPI from enum import Enum from pydantic import BaseModel from typing import Union app = FastAPI() class ModelName(str,Enum): name = "heiheihei" hobby = "DBJ" class Item(BaseModel): name: str price: float description: Union[str, None] = None @app.get("/") async def my_test(): return {"name":"Hello World"} @app.get("/items/{items_id}") async def item_test(items_id:int): return {"items:id":items_id} @app.get("/models/{model_name}") async def get_model(model_name:ModelName): if model_name is ModelName.name: return {'model_name': model_name, 'message': 'I am HEIHEIHEI'} if model_name.value == "DBJ": return {"model_name": model_name, "message": "I like dbj"} return {"model_name": model_name, 'message': 'lalala'} @app.post("/models/{model_name}") async def get_models(model_name: ModelName, item: Item): result = {"model_name": model_name} if item.description: result.update(item.dict()) return result
标签:return,name,get,FastAPI,app,学习,items,model From: https://www.cnblogs.com/yesirya/p/17041405.html