Skip to content

๐Ÿ’ช - โ„น

โ„น โŽ โฎ๏ธ 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() ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐Ÿ”„, โœ‹๏ธ โšซ๏ธ โš’ ๐Ÿ’ญ (& ๐Ÿ—œ) ๐Ÿ’ฒ ๐Ÿ’ฝ ๐Ÿ†Ž ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ—œ ๐ŸŽป, ๐Ÿ–ผ, 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.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).

๐Ÿ”ฌ โšช๏ธโžก๏ธ ๐Ÿท โฎ๏ธ ๐ŸŒ ๐Ÿ“ฆ ๐Ÿ’ฒ โ„น & ๐Ÿท โฎ๏ธ โœ” ๐Ÿ’ฒ ๐Ÿ—, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ’ญ ๐Ÿ”ฌ โž• ๐Ÿท.