docs_src/body/tutorial002_py310.py
raw| 1 | from fastapi import FastAPI |
| 2 | from pydantic import BaseModel |
| 3 | |
| 4 | |
| 5 | class Item(BaseModel): |
| 6 | name: str |
| 7 | description: str | None = None |
| 8 | price: float |
| 9 | tax: float | None = None |
| 10 | |
| 11 | |
| 12 | app = FastAPI() |
| 13 | |
| 14 | |
| 15 | @app.post("/items/") |
| 16 | async def create_item(item: Item): |
| 17 | item_dict = item.model_dump() |
| 18 | if item.tax is not None: |
| 19 | price_with_tax = item.price + item.tax |
| 20 | item_dict.update({"price_with_tax": price_with_tax}) |
| 21 | return item_dict |