docs_src/encoder/tutorial001_py310.py
raw| 1 | from datetime import datetime |
| 2 | |
| 3 | from fastapi import FastAPI |
| 4 | from fastapi.encoders import jsonable_encoder |
| 5 | from pydantic import BaseModel |
| 6 | |
| 7 | fake_db = {} |
| 8 | |
| 9 | |
| 10 | class Item(BaseModel): |
| 11 | title: str |
| 12 | timestamp: datetime |
| 13 | description: str | None = None |
| 14 | |
| 15 | |
| 16 | app = FastAPI() |
| 17 | |
| 18 | |
| 19 | @app.put("/items/{id}") |
| 20 | def update_item(id: str, item: Item): |
| 21 | json_compatible_item_data = jsonable_encoder(item) |
| 22 | fake_db[id] = json_compatible_item_data |