๐ช - โน¶
โน โ โฎ๏ธ PUT¶
โน ๐ฌ ๐ ๐ช โ๏ธ ๐บ๐ธ๐ PUT ๐ ๏ธ.
๐ ๐ช โ๏ธ jsonable_encoder ๐ ๐ข ๐ฝ ๐ ๐ ๐ช ๐ช ๐ป (โ
 โฎ๏ธ โ ๐ฝ). ๐ผ, ๐ญ datetime str.
from typing import List, Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: List[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.put("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    update_item_encoded = jsonable_encoder(item)
    items[item_id] = update_item_encoded
    return update_item_encoded
from typing import Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.put("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    update_item_encoded = jsonable_encoder(item)
    items[item_id] = update_item_encoded
    return update_item_encoded
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str | None = None
    description: str | None = None
    price: float | None = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.put("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    update_item_encoded = jsonable_encoder(item)
    items[item_id] = update_item_encoded
    return update_item_encoded
PUT โ๏ธ ๐จ ๐ฝ ๐ ๐ โ โป ๐ฝ.
โ ๐ โ¶
๐ โ ๐ ๐ฅ ๐ ๐ โน ๐ฌ bar โ๏ธ PUT โฎ๏ธ ๐ช โ:
{
    "name": "Barz",
    "price": 3,
    "description": None,
}
โฉ๏ธ โซ๏ธ ๐ซ ๐ โช ๐ช ๐ข "tax": 20.2, ๐ข ๐ท ๐ โ ๐ข ๐ฒ "tax": 10.5.
& ๐ ๐ ๐ โฎ๏ธ ๐ "๐" tax 10.5.
๐ โน โฎ๏ธ PATCH¶
๐ ๐ช โ๏ธ ๐บ๐ธ๐ PATCH ๐ ๏ธ ๐ โน ๐ฝ.
๐ โ ๐ ๐ ๐ช ๐จ ๐ด ๐ฝ ๐ ๐ ๐ โน, ๐ ๐ ๐ฃ.
Note
PATCH ๐ ๐ โ๏ธ & ๐ญ ๐ PUT.
& ๐ ๐ โ๏ธ ๐ด PUT, ๐ โน.
๐ ๐ โ๏ธ ๐ซ ๐ ๐ ๐, FastAPI ๐ซ ๐ซ ๐ ๐ซ.
โ๏ธ ๐ ๐ฆฎ ๐ฆ ๐, ๐ โ๏ธ ๐, โ ๐ซ ๐ฏ โ๏ธ.
โ๏ธ Pydantic exclude_unset ๐ข¶
๐ฅ ๐ ๐ ๐จ ๐ โน, โซ๏ธ ๐ถ โ  โ๏ธ ๐ข exclude_unset Pydantic ๐ท .dict().
๐ item.dict(exclude_unset=True).
๐ ๐ ๐ dict โฎ๏ธ ๐ด ๐ฝ ๐ โ ๐โ ๐ item ๐ท, ๐ซ ๐ข ๐ฒ.
โคด๏ธ ๐ ๐ช โ๏ธ ๐ ๐ dict โฎ๏ธ ๐ด ๐ฝ ๐ โ (๐จ ๐จ), ๐ซ ๐ข ๐ฒ:
from typing import List, Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: List[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
from typing import Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str | None = None
    description: str | None = None
    price: float | None = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
โ๏ธ Pydantic update ๐ข¶
๐, ๐ ๐ช โ ๐ โป ๐ท โ๏ธ .copy(), & ๐ถโโ๏ธ update ๐ข โฎ๏ธ dict โ ๐ฝ โน.
๐ stored_item_model.copy(update=update_data):
from typing import List, Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: List[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
from typing import Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str | None = None
    description: str | None = None
    price: float | None = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
๐ โน ๐¶
๐, โ ๐ โน ๐ ๐:
- (โ) โ๏ธ PATCHโฉ๏ธPUT.
- ๐ ๐ช ๐ฝ.
- ๐ฎ ๐ ๐ฝ Pydantic ๐ท.
- ๐ dict๐ต ๐ข ๐ฒ โช๏ธโก๏ธ ๐ข ๐ท (โ๏ธexclude_unset).- ๐ ๐ ๐ ๐ช โน ๐ด ๐ฒ ๐ค โ ๐ฉโ๐ป, โฉ๏ธ ๐ ๐ฒ โช ๐ช โฎ๏ธ ๐ข ๐ฒ ๐ ๐ท.
 
- โ ๐ ๐ช ๐ท, ๐ ๏ธ โซ๏ธ ๐ข โฎ๏ธ ๐จ ๐ โน (โ๏ธ update๐ข).
- ๐ ๐ ๐ท ๐ณ ๐ ๐ช ๐ช ๐ ๐ฝ (๐ผ, โ๏ธ jsonable_encoder).- ๐ โญ โ๏ธ ๐ท .dict()๐ฉโ๐ฌ ๐, โ๏ธ โซ๏ธ โ ๐ญ (& ๐) ๐ฒ ๐ฝ ๐ ๐ ๐ช ๐ ๐ป, ๐ผ,datetimestr.
 
- ๐ โญ โ๏ธ ๐ท 
- ๐ ๐ฝ ๐ ๐ฝ.
- ๐จ โน ๐ท.
from typing import List, Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: List[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
from typing import Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: Union[str, None] = None
    description: Union[str, None] = None
    price: Union[float, None] = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
    name: str | None = None
    description: str | None = None
    price: float | None = None
    tax: float = 10.5
    tags: list[str] = []
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item)
async def read_item(item_id: str):
    return items[item_id]
@app.patch("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item: Item):
    stored_item_data = items[item_id]
    stored_item_model = Item(**stored_item_data)
    update_data = item.dict(exclude_unset=True)
    updated_item = stored_item_model.copy(update=update_data)
    items[item_id] = jsonable_encoder(updated_item)
    return updated_item
Tip
๐ ๐ช ๐ค โ๏ธ ๐ ๐ โ โฎ๏ธ ๐บ๐ธ๐ PUT ๐ ๏ธ.
โ๏ธ ๐ผ ๐ฅ โ๏ธ PATCH โฉ๏ธ โซ๏ธ โ ๐ซ โ๏ธ ๐ผ.
Note
๐ ๐ ๐ข ๐ท โ.
, ๐ฅ ๐ ๐ ๐จ ๐ โน ๐ ๐ช ๐ซ ๐ ๐ข, ๐ ๐ช โ๏ธ ๐ท โฎ๏ธ ๐ ๐ข โข ๐ฆ (โฎ๏ธ ๐ข ๐ฒ โ๏ธ None).
๐ฌ โช๏ธโก๏ธ ๐ท โฎ๏ธ ๐ ๐ฆ ๐ฒ โน & ๐ท โฎ๏ธ โ ๐ฒ ๐, ๐ ๐ช โ๏ธ ๐ญ ๐ฌ โ ๐ท.