๐ข ๐ข & ๐ป ๐ฌ¶
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
๐ฒ.
๐ โญ ๐ ๐ โ ๐ฃ ๐ฌ ๐ ๐, ๐ ๐ข.