Skip to content

๐Ÿ’ช - ๐Ÿ” ๐Ÿท

โฎ๏ธ FastAPI, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ, โœ”, ๐Ÿ“„, & โš™๏ธ ๐ŸŽฒ ๐Ÿ™‡ ๐Ÿฆ ๐Ÿท (๐Ÿ‘ Pydantic).

๐Ÿ“‡ ๐Ÿ‘

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ”ข ๐Ÿพ. ๐Ÿ–ผ, ๐Ÿ list:

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: list = []


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: list = []


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

๐Ÿ‘‰ ๐Ÿ”œ โš’ tags ๐Ÿ“‡, ๐Ÿ‘ โšซ๏ธ ๐Ÿšซ ๐Ÿ“ฃ ๐Ÿ†Ž ๐Ÿ”ฃ ๐Ÿ“‡.

๐Ÿ“‡ ๐Ÿ‘ โฎ๏ธ ๐Ÿ†Ž ๐Ÿ”ข

โœ‹๏ธ ๐Ÿ โœ”๏ธ ๐ŸŽฏ ๐ŸŒŒ ๐Ÿ“ฃ ๐Ÿ“‡ โฎ๏ธ ๐Ÿ”— ๐Ÿ†Ž, โš–๏ธ "๐Ÿ†Ž ๐Ÿ”ข":

๐Ÿ—„ โŒจ List

๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ & ๐Ÿ”› ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿฉ list ๐Ÿ“ฃ ๐Ÿ‘ซ ๐Ÿ†Ž โœ ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ”›. ๐Ÿ‘ถ

โœ‹๏ธ ๐Ÿ โฌ โญ 3๏ธโƒฃ.9๏ธโƒฃ (3๏ธโƒฃ.6๏ธโƒฃ & ๐Ÿ”›), ๐Ÿ‘† ๐Ÿฅ‡ ๐Ÿ’ช ๐Ÿ—„ List โšช๏ธโžก๏ธ ๐Ÿฉ ๐Ÿ typing ๐Ÿ•น:

from typing import List, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: List[str] = []


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

๐Ÿ“ฃ list โฎ๏ธ ๐Ÿ†Ž ๐Ÿ”ข

๐Ÿ“ฃ ๐Ÿ†Ž ๐Ÿ‘ˆ โœ”๏ธ ๐Ÿ†Ž ๐Ÿ”ข (๐Ÿ”— ๐Ÿ†Ž), ๐Ÿ’– list, dict, tuple:

  • ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ โฌ ๐Ÿ”… ๐ŸŒ˜ 3๏ธโƒฃ.9๏ธโƒฃ, ๐Ÿ—„ ๐Ÿ‘ซ ๐ŸŒ“ โฌ โšช๏ธโžก๏ธ typing ๐Ÿ•น
  • ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”— ๐Ÿ†Ž(โ“‚) "๐Ÿ†Ž ๐Ÿ”ข" โš™๏ธ โฌœ ๐Ÿ—œ: [ & ]

๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ โšซ๏ธ ๐Ÿ”œ:

my_list: list[str]

โฌ ๐Ÿ โญ 3๏ธโƒฃ.9๏ธโƒฃ, โšซ๏ธ ๐Ÿ”œ:

from typing import List

my_list: List[str]

๐Ÿ‘ˆ ๐ŸŒ ๐Ÿฉ ๐Ÿ โ• ๐Ÿ†Ž ๐Ÿ“„.

โš™๏ธ ๐Ÿ‘ˆ ๐ŸŽ ๐Ÿฉ โ• ๐Ÿท ๐Ÿ”ข โฎ๏ธ ๐Ÿ”— ๐Ÿ†Ž.

, ๐Ÿ‘† ๐Ÿ–ผ, ๐Ÿ‘ฅ ๐Ÿ’ช โš’ tags ๐ŸŽฏ "๐Ÿ“‡ ๐ŸŽป":

from typing import List, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: List[str] = []


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: list[str] = []


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: list[str] = []


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

โš’ ๐Ÿ†Ž

โœ‹๏ธ โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ’ญ ๐Ÿ”ƒ โšซ๏ธ, & ๐Ÿค” ๐Ÿ‘ˆ ๐Ÿ”– ๐Ÿšซ๐Ÿ”œ ๐Ÿšซ ๐Ÿ”, ๐Ÿ‘ซ ๐Ÿ”œ ๐ŸŽฒ ๐Ÿ˜ ๐ŸŽป.

& ๐Ÿ โœ”๏ธ ๐ŸŽ ๐Ÿ’ฝ ๐Ÿ†Ž โš’ ๐Ÿ˜ ๐Ÿฌ, set.

โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ“ฃ tags โš’ ๐ŸŽป:

from typing import Set, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: set[str] = set()


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

โฎ๏ธ ๐Ÿ‘‰, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ“จ โฎ๏ธ โŽ ๐Ÿ“Š, โšซ๏ธ ๐Ÿ”œ ๐Ÿ—œ โš’ ๐Ÿ˜ ๐Ÿฌ.

& ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ“Š, ๐Ÿšฅ โ„น โœ”๏ธ โŽ, โšซ๏ธ ๐Ÿ”œ ๐Ÿ”ข โš’ ๐Ÿ˜ ๐Ÿฌ.

& โšซ๏ธ ๐Ÿ”œ โœ / ๐Ÿ“„ โžก๏ธ ๐Ÿ’โ€โ™‚๏ธ.

๐Ÿฆ ๐Ÿท

๐Ÿ”  ๐Ÿ”ข Pydantic ๐Ÿท โœ”๏ธ ๐Ÿ†Ž.

โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿ†Ž ๐Ÿ’ช โšซ๏ธ โž•1๏ธโƒฃ Pydantic ๐Ÿท.

, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ™‡ ๐Ÿฆ ๐ŸŽป "๐ŸŽš" โฎ๏ธ ๐ŸŽฏ ๐Ÿ”ข ๐Ÿ“›, ๐Ÿ†Ž & ๐Ÿ”ฌ.

๐ŸŒ ๐Ÿ‘ˆ, ๐ŸŽฒ ๐Ÿฆ.

๐Ÿ”ฌ ๐Ÿ“Š

๐Ÿ–ผ, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ”ฌ Image ๐Ÿท:

from typing import Set, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()
    image: Union[Image, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: set[str] = set()
    image: Union[Image, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()
    image: Image | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

โš™๏ธ ๐Ÿ“Š ๐Ÿ†Ž

& โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ’ช โš™๏ธ โšซ๏ธ ๐Ÿ†Ž ๐Ÿ”ข:

from typing import Set, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()
    image: Union[Image, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: set[str] = set()
    image: Union[Image, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Image(BaseModel):
    url: str
    name: str


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()
    image: Image | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

๐Ÿ‘‰ ๐Ÿ”œ โ›“ ๐Ÿ‘ˆ FastAPI ๐Ÿ”œ โŒ› ๐Ÿ’ช ๐ŸŽ:

{
    "name": "Foo",
    "description": "The pretender",
    "price": 42.0,
    "tax": 3.2,
    "tags": ["rock", "metal", "bar"],
    "image": {
        "url": "http://example.com/baz.jpg",
        "name": "The Foo live"
    }
}

๐Ÿ”„, ๐Ÿคธ ๐Ÿ‘ˆ ๐Ÿ“„, โฎ๏ธ FastAPI ๐Ÿ‘† ๐Ÿคš:

  • ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ (๐Ÿ› ๏ธ, โ™’๏ธ), ๐Ÿฆ ๐Ÿท
  • ๐Ÿ’ฝ ๐Ÿ› ๏ธ
  • ๐Ÿ’ฝ ๐Ÿ”ฌ
  • ๐Ÿง ๐Ÿงพ

๐ŸŽ ๐Ÿ†Ž & ๐Ÿ”ฌ

โ†–๏ธ โšช๏ธโžก๏ธ ๐Ÿ˜ โญ ๐Ÿ†Ž ๐Ÿ’– str, int, float, โ™’๏ธ. ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŒ… ๐Ÿ— โญ ๐Ÿ†Ž ๐Ÿ‘ˆ ๐Ÿ˜– โšช๏ธโžก๏ธ str.

๐Ÿ‘€ ๐ŸŒ ๐ŸŽ› ๐Ÿ‘† โœ”๏ธ, ๐Ÿ›’ ๐Ÿฉบ Pydantic ๐Ÿ˜ ๐Ÿ†Ž. ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ–ผ โญ ๐Ÿ“ƒ.

๐Ÿ–ผ, Image ๐Ÿท ๐Ÿ‘ฅ โœ”๏ธ url ๐Ÿ‘, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ“ฃ โšซ๏ธ โ†ฉ๏ธ str, Pydantic HttpUrl:

from typing import Set, Union

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()
    image: Union[Image, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: set[str] = set()
    image: Union[Image, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()
    image: Image | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

๐ŸŽป ๐Ÿ”œ โœ… โ˜‘ ๐Ÿ“›, & ๐Ÿ“„ ๐ŸŽป ๐Ÿ”— / ๐Ÿ—„ โœ….

๐Ÿ”ข โฎ๏ธ ๐Ÿ“‡ ๐Ÿ“Š

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Pydantic ๐Ÿท ๐Ÿพ list, set, โ™’๏ธ:

from typing import List, Set, Union

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()
    images: Union[List[Image], None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: set[str] = set()
    images: Union[list[Image], None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()
    images: list[Image] | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

๐Ÿ‘‰ ๐Ÿ”œ โŒ› (๐Ÿ—œ, โœ”, ๐Ÿ“„, โ™’๏ธ) ๐ŸŽป ๐Ÿ’ช ๐Ÿ’–:

{
    "name": "Foo",
    "description": "The pretender",
    "price": 42.0,
    "tax": 3.2,
    "tags": [
        "rock",
        "metal",
        "bar"
    ],
    "images": [
        {
            "url": "http://example.com/baz.jpg",
            "name": "The Foo live"
        },
        {
            "url": "http://example.com/dave.jpg",
            "name": "The Baz"
        }
    ]
}

Info

๐Ÿ‘€ โ” images ๐Ÿ”‘ ๐Ÿ”œ โœ”๏ธ ๐Ÿ“‡ ๐Ÿ–ผ ๐ŸŽš.

๐Ÿ™‡ ๐Ÿฆ ๐Ÿท

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐ŸŽฒ ๐Ÿ™‡ ๐Ÿฆ ๐Ÿท:

from typing import List, Set, Union

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()
    images: Union[List[Image], None] = None


class Offer(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    items: List[Item]


@app.post("/offers/")
async def create_offer(offer: Offer):
    return offer
from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: set[str] = set()
    images: Union[list[Image], None] = None


class Offer(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    items: list[Item]


@app.post("/offers/")
async def create_offer(offer: Offer):
    return offer
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()
    images: list[Image] | None = None


class Offer(BaseModel):
    name: str
    description: str | None = None
    price: float
    items: list[Item]


@app.post("/offers/")
async def create_offer(offer: Offer):
    return offer

Info

๐Ÿ‘€ โ” Offer โœ”๏ธ ๐Ÿ“‡ Itemโ“‚, โ” ๐Ÿ”„ โœ”๏ธ ๐Ÿ“ฆ ๐Ÿ“‡ Imageโ“‚

๐Ÿ’ช ๐Ÿ˜ ๐Ÿ“‡

๐Ÿšฅ ๐Ÿ” ๐ŸŽš ๐Ÿ’ฒ ๐ŸŽป ๐Ÿ’ช ๐Ÿ‘† โŒ› ๐ŸŽป array (๐Ÿ list), ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ†Ž ๐Ÿ”ข ๐Ÿ”ข, ๐ŸŽ Pydantic ๐Ÿท:

images: List[Image]

โš–๏ธ ๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ & ๐Ÿ”›:

images: list[Image]

:

from typing import List

from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


@app.post("/images/multiple/")
async def create_multiple_images(images: List[Image]):
    return images
from fastapi import FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Image(BaseModel):
    url: HttpUrl
    name: str


@app.post("/images/multiple/")
async def create_multiple_images(images: list[Image]):
    return images

๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ ๐ŸŒ

& ๐Ÿ‘† ๐Ÿคš ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ ๐ŸŒ.

๐Ÿฌ ๐Ÿ”˜ ๐Ÿ“‡:

๐Ÿ‘† ๐Ÿšซ ๐Ÿšซ ๐Ÿคš ๐Ÿ‘‰ ๐Ÿ˜‡ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ‘ท ๐Ÿ”— โฎ๏ธ dict โ†ฉ๏ธ Pydantic ๐Ÿท.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿšซ โœ”๏ธ ๐Ÿ˜Ÿ ๐Ÿ”ƒ ๐Ÿ‘ซ ๐Ÿ‘ฏโ€โ™‚๏ธ, ๐Ÿ“จ #๏ธโƒฃ ๐Ÿ—œ ๐Ÿ” & ๐Ÿ‘† ๐Ÿ”ข ๐Ÿ—œ ๐Ÿ” ๐ŸŽป ๐Ÿ’โ€โ™‚๏ธ.

๐Ÿ’ช โŒ dictโ“‚

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’ช dict โฎ๏ธ ๐Ÿ”‘ ๐Ÿ†Ž & ๐Ÿ’ฒ ๐ŸŽ ๐Ÿ†Ž.

๐Ÿต โœ”๏ธ ๐Ÿ’ญ โช โšซ๏ธโ” โ˜‘ ๐Ÿ‘/๐Ÿ”ข ๐Ÿ“› (๐Ÿ”œ ๐Ÿ’ผ โฎ๏ธ Pydantic ๐Ÿท).

๐Ÿ‘‰ ๐Ÿ”œ โš  ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ“จ ๐Ÿ”‘ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿšซ โช ๐Ÿ’ญ.


๐ŸŽ โš  ๐Ÿ’ผ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’š โœ”๏ธ ๐Ÿ”‘ ๐ŸŽ ๐Ÿ†Ž, โœ… int.

๐Ÿ‘ˆ โšซ๏ธโ” ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ“ฅ.

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿšซ ๐Ÿ™† dict ๐Ÿ“ โšซ๏ธ โœ”๏ธ int ๐Ÿ”‘ โฎ๏ธ float ๐Ÿ’ฒ:

from typing import Dict

from fastapi import FastAPI

app = FastAPI()


@app.post("/index-weights/")
async def create_index_weights(weights: Dict[int, float]):
    return weights
from fastapi import FastAPI

app = FastAPI()


@app.post("/index-weights/")
async def create_index_weights(weights: dict[int, float]):
    return weights

Tip

โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐ŸŽป ๐Ÿ•ด ๐Ÿ•โ€๐Ÿฆบ str ๐Ÿ”‘.

โœ‹๏ธ Pydantic โœ”๏ธ ๐Ÿง ๐Ÿ’ฝ ๐Ÿ› ๏ธ.

๐Ÿ‘‰ โ›“ ๐Ÿ‘ˆ, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ› ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ’ช ๐Ÿ•ด ๐Ÿ“จ ๐ŸŽป ๐Ÿ”‘, ๐Ÿ“ ๐Ÿ‘ˆ ๐ŸŽป ๐Ÿ”Œ ๐Ÿ˜ ๐Ÿ”ข, Pydantic ๐Ÿ”œ ๐Ÿ—œ ๐Ÿ‘ซ & โœ” ๐Ÿ‘ซ.

& dict ๐Ÿ‘† ๐Ÿ“จ weights ๐Ÿ”œ ๐Ÿค™ โœ”๏ธ int ๐Ÿ”‘ & float ๐Ÿ’ฒ.

๐ŸŒƒ

โฎ๏ธ FastAPI ๐Ÿ‘† โœ”๏ธ ๐Ÿ”† ๐Ÿ’ช ๐Ÿšš Pydantic ๐Ÿท, โช ๐Ÿšง ๐Ÿ‘† ๐Ÿ“Ÿ ๐Ÿ™…, ๐Ÿ“ & ๐Ÿ˜.

โœ‹๏ธ โฎ๏ธ ๐ŸŒ ๐Ÿ’ฐ:

  • ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ (๐Ÿ› ๏ธ ๐ŸŒ โ—)
  • ๐Ÿ’ฝ ๐Ÿ› ๏ธ (.โ“‚.. โœ / ๐Ÿ› ๏ธ)
  • ๐Ÿ’ฝ ๐Ÿ”ฌ
  • ๐Ÿ”— ๐Ÿงพ
  • ๐Ÿง ๐Ÿฉบ