Skip to content

Body — Updates

  • PUT — full replacement. The client sends the entire resource.
  • PATCH — partial update. The client sends only the fields to change.
from pydantic import BaseModel
from justapi import JustAPIApp
class Item(BaseModel):
name: str
price: float
description: str = ""
app = JustAPIApp()
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_id": item_id, **item.model_dump()}

Use exclude_unset=True to get only the fields the client explicitly sent:

@app.patch("/items/{item_id}")
def partial_update(item_id: int, item: Item):
update_data = item.model_dump(exclude_unset=True)
# update_data only contains fields the client sent
return {"item_id": item_id, "updates": update_data}

If the client sends {"name": "New Name"}, only name is in update_data.

from typing import Optional
class ItemUpdate(BaseModel):
name: Optional[str] = None
price: Optional[float] = None
description: Optional[str] = None
@app.patch("/items/{item_id}")
def partial_update(item_id: int, item: ItemUpdate):
update_data = item.model_dump(exclude_unset=True)
return {"item_id": item_id, "updates": update_data}