Skip to content

๐Ÿ”ข ๐Ÿ”ข & ๐ŸŽป ๐Ÿ”ฌ

FastAPI โœ” ๐Ÿ‘† ๐Ÿ“ฃ ๐ŸŒ– โ„น & ๐Ÿ”ฌ ๐Ÿ‘† ๐Ÿ”ข.

โžก๏ธ โœŠ ๐Ÿ‘‰ ๐Ÿˆธ ๐Ÿ–ผ:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = None):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿ”ข ๐Ÿ”ข q ๐Ÿ†Ž Union[str, None] (โš–๏ธ str | None ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ), ๐Ÿ‘ˆ โ›“ ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿ†Ž str โœ‹๏ธ ๐Ÿ’ช None, & ๐Ÿ‘, ๐Ÿ”ข ๐Ÿ’ฒ None, FastAPI ๐Ÿ”œ ๐Ÿ’ญ โšซ๏ธ ๐Ÿšซ โœ”.

Note

FastAPI ๐Ÿ”œ ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ’ฒ q ๐Ÿšซ โœ” โ†ฉ๏ธ ๐Ÿ”ข ๐Ÿ’ฒ = None.

Union Union[str, None] ๐Ÿ”œ โœ” ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿค ๐Ÿ‘† ๐Ÿ‘ ๐Ÿ•โ€๐Ÿฆบ & ๐Ÿ” โŒ.

๐ŸŒ– ๐Ÿ”ฌ

๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ› ๏ธ ๐Ÿ‘ˆ โœ‹๏ธ q ๐Ÿ“ฆ, ๐Ÿ•โ” โšซ๏ธ ๐Ÿšš, ๐Ÿšฎ ๐Ÿ“ ๐Ÿšซ ๐Ÿ“‰ 5๏ธโƒฃ0๏ธโƒฃ ๐Ÿฆน.

๐Ÿ—„ Query

๐Ÿ† ๐Ÿ‘ˆ, ๐Ÿฅ‡ ๐Ÿ—„ Query โšช๏ธโžก๏ธ fastapi:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

โš™๏ธ Query ๐Ÿ”ข ๐Ÿ’ฒ

& ๐Ÿ”œ โš™๏ธ โšซ๏ธ ๐Ÿ”ข ๐Ÿ’ฒ ๐Ÿ‘† ๐Ÿ”ข, โš’ ๐Ÿ”ข max_length 5๏ธโƒฃ0๏ธโƒฃ:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿ‘ฅ โœ”๏ธ โŽ ๐Ÿ”ข ๐Ÿ’ฒ None ๐Ÿ”ข โฎ๏ธ Query(), ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ”œ โš’ ๐Ÿ”ข ๐Ÿ’ฒ โฎ๏ธ ๐Ÿ”ข Query(default=None), โšซ๏ธ ๐Ÿฆ ๐ŸŽ ๐ŸŽฏ โš– ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ’ฒ.

:

q: Union[str, None] = Query(default=None)

...โš’ ๐Ÿ”ข ๐Ÿ“ฆ, ๐ŸŽ:

q: Union[str, None] = None

& ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ & ๐Ÿ”›:

q: str | None = Query(default=None)

...โš’ ๐Ÿ”ข ๐Ÿ“ฆ, ๐ŸŽ:

q: str | None = None

โœ‹๏ธ โšซ๏ธ ๐Ÿ“ฃ โšซ๏ธ ๐ŸŽฏ ๐Ÿ’†โ€โ™‚ ๐Ÿ”ข ๐Ÿ”ข.

Info

โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐ŸŒ… โš  ๐Ÿ• โš’ ๐Ÿ”ข ๐Ÿ“ฆ ๐Ÿ•:

= None

โš–๏ธ:

= Query(default=None)

โšซ๏ธ ๐Ÿ”œ โš™๏ธ ๐Ÿ‘ˆ None ๐Ÿ”ข ๐Ÿ’ฒ, & ๐Ÿ‘ˆ ๐ŸŒŒ โš’ ๐Ÿ”ข ๐Ÿšซ โœ”.

Union[str, None] ๐Ÿ• โœ” ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿšš ๐Ÿ‘ป ๐Ÿ•โ€๐Ÿฆบ, โœ‹๏ธ โšซ๏ธ ๐Ÿšซ โšซ๏ธโ” ๐Ÿ’ฌ FastAPI ๐Ÿ‘ˆ ๐Ÿ‘‰ ๐Ÿ”ข ๐Ÿšซ โœ”.

โคด๏ธ, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐ŸŒ… ๐Ÿ”ข Query. ๐Ÿ‘‰ ๐Ÿ’ผ, max_length ๐Ÿ”ข ๐Ÿ‘ˆ โœ” ๐ŸŽป:

q: Union[str, None] = Query(default=None, max_length=50)

๐Ÿ‘‰ ๐Ÿ”œ โœ” ๐Ÿ“Š, ๐ŸŽฆ ๐Ÿ†‘ โŒ ๐Ÿ•โ” ๐Ÿ“Š ๐Ÿšซ โ˜‘, & ๐Ÿ“„ ๐Ÿ”ข ๐Ÿ—„ ๐Ÿ”— โžก ๐Ÿ› ๏ธ.

๐Ÿšฎ ๐ŸŒ… ๐Ÿ”ฌ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ”ข min_length:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Union[str, None] = Query(default=None, min_length=3, max_length=50)
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, min_length=3, max_length=50)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿšฎ ๐Ÿฅ” ๐Ÿงฌ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿฅ” ๐Ÿงฌ ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ”œ ๐Ÿ:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Union[str, None] = Query(
        default=None, min_length=3, max_length=50, pattern="^fixedquery$"
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: str | None = Query(
        default=None, min_length=3, max_length=50, pattern="^fixedquery$"
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿ‘‰ ๐ŸŽฏ ๐Ÿฅ” ๐Ÿงฌ โœ… ๐Ÿ‘ˆ ๐Ÿ“จ ๐Ÿ”ข ๐Ÿ’ฒ:

  • ^: โ–ถ๏ธ โฎ๏ธ ๐Ÿ“„ ๐Ÿฆน, ๐Ÿšซ โœ”๏ธ ๐Ÿฆน โญ.
  • fixedquery: โœ”๏ธ โ˜‘ ๐Ÿ’ฒ fixedquery.
  • $: ๐Ÿ”š ๐Ÿ“ค, ๐Ÿšซ โœ”๏ธ ๐Ÿ™† ๐ŸŒ– ๐Ÿฆน โฎ๏ธ fixedquery.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ญ ๐Ÿ’ธ โฎ๏ธ ๐ŸŒ ๐Ÿ‘‰ "๐Ÿฅ” ๐Ÿงฌ" ๐Ÿ’ญ, ๐Ÿšซ ๐Ÿ˜Ÿ. ๐Ÿ‘ซ ๐Ÿ‹๏ธ โ” ๐Ÿ“š ๐Ÿ‘ซ๐Ÿ‘ซ. ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“š ๐Ÿ’ฉ ๐Ÿต ๐Ÿ’†โ€โ™‚ ๐Ÿฅ” ๐Ÿงฌ.

โœ‹๏ธ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘ซ & ๐Ÿšถ & ๐Ÿ’ก ๐Ÿ‘ซ, ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช โช โš™๏ธ ๐Ÿ‘ซ ๐Ÿ”— FastAPI.

๐Ÿ”ข ๐Ÿ’ฒ

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ None ๐Ÿ’ฒ default ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐ŸŽ ๐Ÿ’ฒ.

โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ“ฃ q ๐Ÿ”ข ๐Ÿ”ข โœ”๏ธ min_length 3, & โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ "fixedquery":

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(default="fixedquery", min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Note

โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ โš’ ๐Ÿ”ข ๐Ÿ“ฆ.

โš’ โšซ๏ธ โœ”

๐Ÿ•โ” ๐Ÿ‘ฅ ๐Ÿšซ ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ… ๐Ÿ”ฌ โš–๏ธ ๐Ÿ—ƒ, ๐Ÿ‘ฅ ๐Ÿ’ช โš’ q ๐Ÿ”ข ๐Ÿ”ข โœ” ๐Ÿšซ ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ’ฒ, ๐Ÿ’–:

q: str

โ†ฉ๏ธ:

q: Union[str, None] = None

โœ‹๏ธ ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ“ฃ โšซ๏ธ โฎ๏ธ Query, ๐Ÿ–ผ ๐Ÿ’–:

q: Union[str, None] = Query(default=None, min_length=3)

, ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’ฒ โœ” โช โš™๏ธ Query, ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽฏ ๐Ÿšซ ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ’ฒ:

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

โœ” โฎ๏ธ โ• (...)

๐Ÿ“ค ๐ŸŽ› ๐ŸŒŒ ๐ŸŽฏ ๐Ÿ“ฃ ๐Ÿ‘ˆ ๐Ÿ’ฒ โœ”. ๐Ÿ‘† ๐Ÿ’ช โš’ default ๐Ÿ”ข ๐Ÿ”‘ ๐Ÿ’ฒ ...:

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(default=..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Info

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ‘€ ๐Ÿ‘ˆ ... โญ: โšซ๏ธ ๐ŸŽ ๐Ÿ‘ ๐Ÿ’ฒ, โšซ๏ธ ๐Ÿ• ๐Ÿ & ๐Ÿค™ "โ•".

โšซ๏ธ โš™๏ธ Pydantic & FastAPI ๐ŸŽฏ ๐Ÿ“ฃ ๐Ÿ‘ˆ ๐Ÿ’ฒ โœ”.

๐Ÿ‘‰ ๐Ÿ”œ โžก๏ธ FastAPI ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ‘‰ ๐Ÿ”ข โœ”.

โœ” โฎ๏ธ None

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ’ช ๐Ÿšซ None, โœ‹๏ธ ๐Ÿ‘ˆ โšซ๏ธ โœ”. ๐Ÿ‘‰ ๐Ÿ”œ โšก ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ ๐Ÿ’ฒ, ๐Ÿšฅ ๐Ÿ’ฒ None.

๐Ÿ‘ˆ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ‘ˆ None โ˜‘ ๐Ÿ†Ž โœ‹๏ธ โš™๏ธ default=...:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=..., min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Tip

Pydantic, โ” โšซ๏ธโ” ๐Ÿ‹๏ธ ๐ŸŒ ๐Ÿ’ฝ ๐Ÿ”ฌ & ๐Ÿ› ๏ธ FastAPI, โœ”๏ธ ๐ŸŽ ๐ŸŽญ ๐Ÿ•โ” ๐Ÿ‘† โš™๏ธ Optional โš–๏ธ Union[Something, None] ๐Ÿต ๐Ÿ”ข ๐Ÿ’ฒ, ๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ… ๐Ÿ”ƒ โšซ๏ธ Pydantic ๐Ÿฉบ ๐Ÿ”ƒ โœ” ๐Ÿ“ฆ ๐Ÿ‘.

โš™๏ธ Pydantic Required โ†ฉ๏ธ โ• (...)

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ญ ๐Ÿ˜ฌ โš™๏ธ ..., ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ—„ & โš™๏ธ Required โšช๏ธโžก๏ธ Pydantic:

from fastapi import FastAPI, Query
from pydantic import Required

app = FastAPI()


@app.get("/items/")
async def read_items(q: str = Query(default=Required, min_length=3)):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

Tip

๐Ÿ’ญ ๐Ÿ‘ˆ ๐ŸŒ… ๐Ÿ’ผ, ๐Ÿ•โ” ๐Ÿ•ณ ๐Ÿšš, ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽฏ ๐Ÿšซ default ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿ›Ž ๐Ÿšซ โœ”๏ธ โš™๏ธ ... ๐Ÿšซ Required.

๐Ÿ”ข ๐Ÿ”ข ๐Ÿ“‡ / ๐Ÿ’— ๐Ÿ’ฒ

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ”ฌ ๐Ÿ”ข ๐Ÿ”ข ๐ŸŽฏ โฎ๏ธ Query ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ โšซ๏ธ ๐Ÿ“จ ๐Ÿ“‡ ๐Ÿ’ฒ, โš–๏ธ ๐Ÿ™†โ€โ™€ ๐ŸŽ ๐ŸŒŒ, ๐Ÿ“จ ๐Ÿ’— ๐Ÿ’ฒ.

๐Ÿ–ผ, ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ”ข q ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ˜‘ ๐Ÿ’— ๐Ÿ•ฐ ๐Ÿ“›, ๐Ÿ‘† ๐Ÿ’ช โœ:

from typing import List, Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[List[str], None] = Query(default=None)):
    query_items = {"q": q}
    return query_items
from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[list[str], None] = Query(default=None)):
    query_items = {"q": q}
    return query_items
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: list[str] | None = Query(default=None)):
    query_items = {"q": q}
    return query_items

โคด๏ธ, โฎ๏ธ ๐Ÿ“› ๐Ÿ’–:

http://localhost:8000/items/?q=foo&q=bar

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ’— q ๐Ÿ”ข ๐Ÿ”ข' ๐Ÿ’ฒ (foo & bar) ๐Ÿ list ๐Ÿ”˜ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, ๐Ÿ”ข ๐Ÿ”ข q.

, ๐Ÿ“จ ๐Ÿ‘ˆ ๐Ÿ“› ๐Ÿ”œ:

{
  "q": [
    "foo",
    "bar"
  ]
}

Tip

๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ”ข โฎ๏ธ ๐Ÿ†Ž list, ๐Ÿ’– ๐Ÿ–ผ ๐Ÿ”›, ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽฏ โš™๏ธ Query, โช โšซ๏ธ ๐Ÿ”œ ๐Ÿ”ฌ ๐Ÿ“จ ๐Ÿ’ช.

๐ŸŽ“ ๐Ÿ› ๏ธ ๐Ÿฉบ ๐Ÿ”œ โ„น โžก๏ธ, โœ” ๐Ÿ’— ๐Ÿ’ฒ:

๐Ÿ”ข ๐Ÿ”ข ๐Ÿ“‡ / ๐Ÿ’— ๐Ÿ’ฒ โฎ๏ธ ๐Ÿ”ข

& ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ”ข list ๐Ÿ’ฒ ๐Ÿšฅ ๐Ÿ‘Œ ๐Ÿšš:

from typing import List

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: List[str] = Query(default=["foo", "bar"])):
    query_items = {"q": q}
    return query_items
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: list[str] = Query(default=["foo", "bar"])):
    query_items = {"q": q}
    return query_items

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšถ:

http://localhost:8000/items/

๐Ÿ”ข q ๐Ÿ”œ: ["foo", "bar"] & ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ”œ:

{
  "q": [
    "foo",
    "bar"
  ]
}

โš™๏ธ list

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ list ๐Ÿ”— โ†ฉ๏ธ List[str] (โš–๏ธ list[str] ๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ โž•):

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: list = Query(default=[])):
    query_items = {"q": q}
    return query_items

Note

โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐Ÿ‘‰ ๐Ÿ’ผ, FastAPI ๐Ÿ† ๐Ÿšซ โœ… ๐ŸŽš ๐Ÿ“‡.

๐Ÿ–ผ, List[int] ๐Ÿ”œ โœ… (& ๐Ÿ“„) ๐Ÿ‘ˆ ๐ŸŽš ๐Ÿ“‡ ๐Ÿ”ข. โœ‹๏ธ list ๐Ÿ˜ž ๐Ÿšซ๐Ÿ”œ.

๐Ÿ“ฃ ๐ŸŒ… ๐Ÿ—ƒ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐ŸŒ… โ„น ๐Ÿ”ƒ ๐Ÿ”ข.

๐Ÿ‘ˆ โ„น ๐Ÿ”œ ๐Ÿ”Œ ๐Ÿ— ๐Ÿ—„ & โš™๏ธ ๐Ÿงพ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข & ๐Ÿ”ข ๐Ÿงฐ.

Note

โœ”๏ธ ๐Ÿคฏ ๐Ÿ‘ˆ ๐ŸŽ ๐Ÿงฐ 5๏ธโƒฃ๐Ÿ“† โœ”๏ธ ๐ŸŽ ๐ŸŽš ๐Ÿ—„ ๐Ÿ•โ€๐Ÿฆบ.

๐Ÿ‘ซ ๐Ÿ’ช ๐Ÿšซ ๐ŸŽฆ ๐ŸŒ โž• โ„น ๐Ÿ“ฃ, ๐Ÿ‘ ๐ŸŒ… ๐Ÿ’ผ, โŒ โš’ โช ๐Ÿ“„ ๐Ÿ› ๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ title:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Union[str, None] = Query(default=None, title="Query string", min_length=3)
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: str | None = Query(default=None, title="Query string", min_length=3)
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

& description:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Union[str, None] = Query(
        default=None,
        title="Query string",
        description="Query string for the items to search in the database that have a good match",
        min_length=3,
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: str | None = Query(
        default=None,
        title="Query string",
        description="Query string for the items to search in the database that have a good match",
        min_length=3,
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿ“› ๐Ÿ”ข

๐ŸŒˆ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’š ๐Ÿ”ข item-query.

๐Ÿ’–:

http://127.0.0.1:8000/items/?item-query=foobaritems

โœ‹๏ธ item-query ๐Ÿšซ โ˜‘ ๐Ÿ ๐Ÿ”ข ๐Ÿ“›.

๐Ÿ” ๐Ÿ”œ item_query.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช โšซ๏ธ โšซ๏ธโ” item-query...

โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ alias, & ๐Ÿ‘ˆ ๐Ÿ“› โšซ๏ธโ” ๐Ÿ”œ โš™๏ธ ๐Ÿ”Ž ๐Ÿ”ข ๐Ÿ’ฒ:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: Union[str, None] = Query(default=None, alias="item-query")):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(q: str | None = Query(default=None, alias="item-query")):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿ˜› ๐Ÿ”ข

๐Ÿ”œ โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’– ๐Ÿ‘‰ ๐Ÿ”ข ๐Ÿšซ๐Ÿ”œ.

๐Ÿ‘† โœ”๏ธ ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿ“ค โช โ†ฉ๏ธ ๐Ÿ“ค ๐Ÿ‘ฉโ€๐Ÿ’ป โš™๏ธ โšซ๏ธ, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’š ๐Ÿฉบ ๐ŸŽฏ ๐ŸŽฆ โšซ๏ธ ๐Ÿ˜ข.

โคด๏ธ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”ข deprecated=True Query:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: Union[str, None] = Query(
        default=None,
        alias="item-query",
        title="Query string",
        description="Query string for the items to search in the database that have a good match",
        min_length=3,
        max_length=50,
        pattern="^fixedquery$",
        deprecated=True,
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    q: str | None = Query(
        default=None,
        alias="item-query",
        title="Query string",
        description="Query string for the items to search in the database that have a good match",
        min_length=3,
        max_length=50,
        pattern="^fixedquery$",
        deprecated=True,
    ),
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results

๐Ÿฉบ ๐Ÿ”œ ๐ŸŽฆ โšซ๏ธ ๐Ÿ’– ๐Ÿ‘‰:

๐Ÿšซ โšช๏ธโžก๏ธ ๐Ÿ—„

๐Ÿšซ ๐Ÿ”ข ๐Ÿ”ข โšช๏ธโžก๏ธ ๐Ÿ— ๐Ÿ—„ ๐Ÿ”— (& โžก๏ธ, โšช๏ธโžก๏ธ ๐Ÿง ๐Ÿงพ โš™๏ธ), โš’ ๐Ÿ”ข include_in_schema Query False:

from typing import Union

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    hidden_query: Union[str, None] = Query(default=None, include_in_schema=False)
):
    if hidden_query:
        return {"hidden_query": hidden_query}
    else:
        return {"hidden_query": "Not found"}
from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
    hidden_query: str | None = Query(default=None, include_in_schema=False)
):
    if hidden_query:
        return {"hidden_query": hidden_query}
    else:
        return {"hidden_query": "Not found"}

๐ŸŒƒ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ– ๐Ÿ”ฌ & ๐Ÿ—ƒ ๐Ÿ‘† ๐Ÿ”ข.

๐Ÿ’Š ๐Ÿ”ฌ & ๐Ÿ—ƒ:

  • alias
  • title
  • description
  • deprecated

๐Ÿ”ฌ ๐ŸŽฏ ๐ŸŽป:

  • min_length
  • max_length
  • regex

๐Ÿ‘ซ ๐Ÿ–ผ ๐Ÿ‘† ๐Ÿ‘€ โ” ๐Ÿ“ฃ ๐Ÿ”ฌ str ๐Ÿ’ฒ.

๐Ÿ‘€ โญ ๐Ÿ“ƒ ๐Ÿ‘€ โ” ๐Ÿ“ฃ ๐Ÿ”ฌ ๐ŸŽ ๐Ÿ†Ž, ๐Ÿ’– ๐Ÿ”ข.