Browse Source

Update Pydantic example in tutorial to Pydantic v2 syntax

This PR updates outdated code examples in body tutorial docs that still used Pydantic v1 patterns. Updated to Pydantic v2 syntax including:
- Replacing `.dict()` with `.model_dump()`
- Maintaining Python 3.10+ union types (`str | None`)

This ensures documentation is consistent with current FastAPI + Pydantic v2 behavior.
pull/14421/head
Bharathis28 8 months ago
parent
commit
e0391ac7b5
  1. 2
      docs_src/body/tutorial002_py310.py
  2. 2
      docs_src/body/tutorial003_py310.py
  3. 2
      docs_src/body/tutorial004_py310.py

2
docs_src/body/tutorial002_py310.py

@ -14,7 +14,7 @@ app = FastAPI()
@app.post("/items/") @app.post("/items/")
async def create_item(item: Item): async def create_item(item: Item):
item_dict = item.dict() item_dict = item.model_dump()
if item.tax is not None: if item.tax is not None:
price_with_tax = item.price + item.tax price_with_tax = item.price + item.tax
item_dict.update({"price_with_tax": price_with_tax}) item_dict.update({"price_with_tax": price_with_tax})

2
docs_src/body/tutorial003_py310.py

@ -14,4 +14,4 @@ app = FastAPI()
@app.put("/items/{item_id}") @app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item): async def update_item(item_id: int, item: Item):
return {"item_id": item_id, **item.dict()} return {"item_id": item_id, **item.model_dump()}

2
docs_src/body/tutorial004_py310.py

@ -14,7 +14,7 @@ app = FastAPI()
@app.put("/items/{item_id}") @app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, q: str | None = None): async def update_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()} result = {"item_id": item_id, **item.model_dump()}
if q: if q:
result.update({"q": q}) result.update({"q": q})
return result return result

Loading…
Cancel
Save