🔢 🔢 & 🎻 🔬¶
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
👉 🔜 ➡️ 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
💲.
👀 ⏭ 📃 👀 ❔ 📣 🔬 🎏 🆎, 💖 🔢.