12 KiB
🔢 🔢 & 🎻 🔬
FastAPI ✔ 👆 📣 🌖 ℹ & 🔬 👆 🔢.
➡️ ✊ 👉 🈸 🖼:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial001.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial001_py310.py!}
```
🔢 🔢 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
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="3"
{!> ../../../docs_src/query_params_str_validations/tutorial002.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="1"
{!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
```
⚙️ Query
🔢 💲
& 🔜 ⚙️ ⚫️ 🔢 💲 👆 🔢, ⚒ 🔢 max_length
5️⃣0️⃣:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial002.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
```
👥 ✔️ ❎ 🔢 💲 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 ✔️ 🤯 👈 🌅 ⚠ 🍕 ⚒ 🔢 📦 🍕:
```Python
= None
```
⚖️:
```Python
= Query(default=None)
```
⚫️ 🔜 ⚙️ 👈 `None` 🔢 💲, & 👈 🌌 ⚒ 🔢 **🚫 ✔**.
`Union[str, None]` 🍕 ✔ 👆 👨🎨 🚚 👻 🐕🦺, ✋️ ⚫️ 🚫 ⚫️❔ 💬 FastAPI 👈 👉 🔢 🚫 ✔.
⤴️, 👥 💪 🚶♀️ 🌅 🔢 Query
. 👉 💼, max_length
🔢 👈 ✔ 🎻:
q: Union[str, None] = Query(default=None, max_length=50)
👉 🔜 ✔ 📊, 🎦 🆑 ❌ 🕐❔ 📊 🚫 ☑, & 📄 🔢 🗄 🔗 ➡ 🛠️.
🚮 🌅 🔬
👆 💪 🚮 🔢 min_length
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial003.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial003_py310.py!}
```
🚮 🥔 🧬
👆 💪 🔬 🥔 🧬 👈 🔢 🔜 🏏:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="11"
{!> ../../../docs_src/query_params_str_validations/tutorial004.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial004_py310.py!}
```
👉 🎯 🥔 🧬 ✅ 👈 📨 🔢 💲:
^
: ▶️ ⏮️ 📄 🦹, 🚫 ✔️ 🦹 ⏭.fixedquery
: ✔️ ☑ 💲fixedquery
.$
: 🔚 📤, 🚫 ✔️ 🙆 🌖 🦹 ⏮️fixedquery
.
🚥 👆 💭 💸 ⏮️ 🌐 👉 "🥔 🧬" 💭, 🚫 😟. 👫 🏋️ ❔ 📚 👫👫. 👆 💪 📚 💩 🍵 💆♂ 🥔 🧬.
✋️ 🕐❔ 👆 💪 👫 & 🚶 & 💡 👫, 💭 👈 👆 💪 ⏪ ⚙️ 👫 🔗 FastAPI.
🔢 💲
🎏 🌌 👈 👆 💪 🚶♀️ None
💲 default
🔢, 👆 💪 🚶♀️ 🎏 💲.
➡️ 💬 👈 👆 💚 📣 q
🔢 🔢 ✔️ min_length
3
, & ✔️ 🔢 💲 "fixedquery"
:
{!../../../docs_src/query_params_str_validations/tutorial005.py!}
!!! note ✔️ 🔢 💲 ⚒ 🔢 📦.
⚒ ⚫️ ✔
🕐❔ 👥 🚫 💪 📣 🌅 🔬 ⚖️ 🗃, 👥 💪 ⚒ q
🔢 🔢 ✔ 🚫 📣 🔢 💲, 💖:
q: str
↩️:
q: Union[str, None] = None
✋️ 👥 🔜 📣 ⚫️ ⏮️ Query
, 🖼 💖:
q: Union[str, None] = Query(default=None, min_length=3)
, 🕐❔ 👆 💪 📣 💲 ✔ ⏪ ⚙️ Query
, 👆 💪 🎯 🚫 📣 🔢 💲:
{!../../../docs_src/query_params_str_validations/tutorial006.py!}
✔ ⏮️ ❕ (...
)
📤 🎛 🌌 🎯 📣 👈 💲 ✔. 👆 💪 ⚒ default
🔢 🔑 💲 ...
:
{!../../../docs_src/query_params_str_validations/tutorial006b.py!}
!!! info
🚥 👆 🚫 👀 👈 ...
⏭: ⚫️ 🎁 👁 💲, ⚫️ 🍕 🐍 & 🤙 "❕".
⚫️ ⚙️ Pydantic & FastAPI 🎯 📣 👈 💲 ✔.
👉 🔜 ➡️ FastAPI 💭 👈 👉 🔢 ✔.
✔ ⏮️ None
👆 💪 📣 👈 🔢 💪 🚫 None
, ✋️ 👈 ⚫️ ✔. 👉 🔜 ⚡ 👩💻 📨 💲, 🚥 💲 None
.
👈, 👆 💪 📣 👈 None
☑ 🆎 ✋️ ⚙️ default=...
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial006c.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial006c_py310.py!}
```
!!! tip
Pydantic, ❔ ⚫️❔ 🏋️ 🌐 💽 🔬 & 🛠️ FastAPI, ✔️ 🎁 🎭 🕐❔ 👆 ⚙️ Optional
⚖️ Union[Something, None]
🍵 🔢 💲, 👆 💪 ✍ 🌅 🔃 ⚫️ Pydantic 🩺 🔃 ✔ 📦 🏑.
⚙️ Pydantic Required
↩️ ❕ (...
)
🚥 👆 💭 😬 ⚙️ ...
, 👆 💪 🗄 & ⚙️ Required
⚪️➡️ Pydantic:
{!../../../docs_src/query_params_str_validations/tutorial006d.py!}
!!! tip
💭 👈 🌅 💼, 🕐❔ 🕳 🚚, 👆 💪 🎯 🚫 default
🔢, 👆 🛎 🚫 ✔️ ⚙️ ...
🚫 Required
.
🔢 🔢 📇 / 💗 💲
🕐❔ 👆 🔬 🔢 🔢 🎯 ⏮️ Query
👆 💪 📣 ⚫️ 📨 📇 💲, ⚖️ 🙆♀ 🎏 🌌, 📨 💗 💲.
🖼, 📣 🔢 🔢 q
👈 💪 😑 💗 🕰 📛, 👆 💪 ✍:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial011.py!}
```
=== "🐍 3️⃣.9️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial011_py39.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial011_py310.py!}
```
⤴️, ⏮️ 📛 💖:
http://localhost:8000/items/?q=foo&q=bar
👆 🔜 📨 💗 q
🔢 🔢' 💲 (foo
& bar
) 🐍 list
🔘 👆 ➡ 🛠️ 🔢, 🔢 🔢 q
.
, 📨 👈 📛 🔜:
{
"q": [
"foo",
"bar"
]
}
!!! tip
📣 🔢 🔢 ⏮️ 🆎 list
, 💖 🖼 🔛, 👆 💪 🎯 ⚙️ Query
, ⏪ ⚫️ 🔜 🔬 📨 💪.
🎓 🛠️ 🩺 🔜 ℹ ➡️, ✔ 💗 💲:

🔢 🔢 📇 / 💗 💲 ⏮️ 🔢
& 👆 💪 🔬 🔢 list
💲 🚥 👌 🚚:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial012.py!}
```
=== "🐍 3️⃣.9️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial012_py39.py!}
```
🚥 👆 🚶:
http://localhost:8000/items/
🔢 q
🔜: ["foo", "bar"]
& 👆 📨 🔜:
{
"q": [
"foo",
"bar"
]
}
⚙️ list
👆 💪 ⚙️ list
🔗 ↩️ List[str]
(⚖️ list[str]
🐍 3️⃣.9️⃣ ➕):
{!../../../docs_src/query_params_str_validations/tutorial013.py!}
!!! note ✔️ 🤯 👈 👉 💼, FastAPI 🏆 🚫 ✅ 🎚 📇.
🖼, `List[int]` 🔜 ✅ (& 📄) 👈 🎚 📇 🔢. ✋️ `list` 😞 🚫🔜.
📣 🌅 🗃
👆 💪 🚮 🌅 ℹ 🔃 🔢.
👈 ℹ 🔜 🔌 🏗 🗄 & ⚙️ 🧾 👩💻 🔢 & 🔢 🧰.
!!! note ✔️ 🤯 👈 🎏 🧰 5️⃣📆 ✔️ 🎏 🎚 🗄 🐕🦺.
👫 💪 🚫 🎦 🌐 ➕ ℹ 📣, 👐 🌅 💼, ❌ ⚒ ⏪ 📄 🛠️.
👆 💪 🚮 title
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial007.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/query_params_str_validations/tutorial007_py310.py!}
```
& description
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="13"
{!> ../../../docs_src/query_params_str_validations/tutorial008.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="12"
{!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
```
📛 🔢
🌈 👈 👆 💚 🔢 item-query
.
💖:
http://127.0.0.1:8000/items/?item-query=foobaritems
✋️ item-query
🚫 ☑ 🐍 🔢 📛.
🔐 🔜 item_query
.
✋️ 👆 💪 ⚫️ ⚫️❔ item-query
...
⤴️ 👆 💪 📣 alias
, & 👈 📛 ⚫️❔ 🔜 ⚙️ 🔎 🔢 💲:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial009.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial009_py310.py!}
```
😛 🔢
🔜 ➡️ 💬 👆 🚫 💖 👉 🔢 🚫🔜.
👆 ✔️ 👈 ⚫️ 📤 ⏪ ↩️ 📤 👩💻 ⚙️ ⚫️, ✋️ 👆 💚 🩺 🎯 🎦 ⚫️ 😢.
⤴️ 🚶♀️ 🔢 deprecated=True
Query
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/query_params_str_validations/tutorial010.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="17"
{!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
```
🩺 🔜 🎦 ⚫️ 💖 👉:

🚫 ⚪️➡️ 🗄
🚫 🔢 🔢 ⚪️➡️ 🏗 🗄 🔗 (& ➡️, ⚪️➡️ 🏧 🧾 ⚙️), ⚒ 🔢 include_in_schema
Query
False
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial014.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/query_params_str_validations/tutorial014_py310.py!}
```
🌃
👆 💪 📣 🌖 🔬 & 🗃 👆 🔢.
💊 🔬 & 🗃:
alias
title
description
deprecated
🔬 🎯 🎻:
min_length
max_length
regex
👫 🖼 👆 👀 ❔ 📣 🔬 str
💲.
👀 ⏭ 📃 👀 ❔ 📣 🔬 🎏 🆎, 💖 🔢.