docs_src/response_model/tutorial001_01_py310.py
raw| 1 | from fastapi import FastAPI |
| 2 | from pydantic import BaseModel |
| 3 | |
| 4 | app = FastAPI() |
| 5 | |
| 6 | |
| 7 | class Item(BaseModel): |
| 8 | name: str |
| 9 | description: str | None = None |
| 10 | price: float |
| 11 | tax: float | None = None |
| 12 | tags: list[str] = [] |
| 13 | |
| 14 | |
| 15 | @app.post("/items/") |
| 16 | async def create_item(item: Item) -> Item: |
| 17 | return item |
| 18 | |
| 19 | |
| 20 | @app.get("/items/") |
| 21 | async def read_items() -> list[Item]: |
| 22 | return [ |
| 23 | Item(name="Portal Gun", price=42.0), |
| 24 | Item(name="Plumbus", price=32.0), |
| 25 | ] |