mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-02 02:57:57 -05:00
* 🌐 💬 🩺 🦲 * 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks * 🛠️😊 * ♻️ Rename emoji lang from emj to em, and main docs name as 😉 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Axd1x8a <26704473+FeeeeK@users.noreply.github.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
468 lines
12 KiB
Markdown
468 lines
12 KiB
Markdown
# 🔢 🔢 & 🎻 🔬
|
||
|
||
**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)`, ⚫️ 🍦 🎏 🎯 ⚖ 👈 🔢 💲.
|
||
|
||
:
|
||
|
||
```Python
|
||
q: Union[str, None] = Query(default=None)
|
||
```
|
||
|
||
...⚒ 🔢 📦, 🎏:
|
||
|
||
```Python
|
||
q: Union[str, None] = None
|
||
```
|
||
|
||
& 🐍 3️⃣.1️⃣0️⃣ & 🔛:
|
||
|
||
```Python
|
||
q: str | None = Query(default=None)
|
||
```
|
||
|
||
...⚒ 🔢 📦, 🎏:
|
||
|
||
```Python
|
||
q: str | None = None
|
||
```
|
||
|
||
✋️ ⚫️ 📣 ⚫️ 🎯 💆♂ 🔢 🔢.
|
||
|
||
!!! info
|
||
✔️ 🤯 👈 🌅 ⚠ 🍕 ⚒ 🔢 📦 🍕:
|
||
|
||
```Python
|
||
= None
|
||
```
|
||
|
||
⚖️:
|
||
|
||
```Python
|
||
= Query(default=None)
|
||
```
|
||
|
||
⚫️ 🔜 ⚙️ 👈 `None` 🔢 💲, & 👈 🌌 ⚒ 🔢 **🚫 ✔**.
|
||
|
||
`Union[str, None]` 🍕 ✔ 👆 👨🎨 🚚 👻 🐕🦺, ✋️ ⚫️ 🚫 ⚫️❔ 💬 FastAPI 👈 👉 🔢 🚫 ✔.
|
||
|
||
⤴️, 👥 💪 🚶♀️ 🌅 🔢 `Query`. 👉 💼, `max_length` 🔢 👈 ✔ 🎻:
|
||
|
||
```Python
|
||
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!}
|
||
```
|
||
|
||
## 🚮 🥔 🧬
|
||
|
||
👆 💪 🔬 <abbr title="A regular expression, regex or regexp is a sequence of characters that define a search pattern for strings.">🥔 🧬</abbr> 👈 🔢 🔜 🏏:
|
||
|
||
=== "🐍 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"`:
|
||
|
||
```Python hl_lines="7"
|
||
{!../../../docs_src/query_params_str_validations/tutorial005.py!}
|
||
```
|
||
|
||
!!! note
|
||
✔️ 🔢 💲 ⚒ 🔢 📦.
|
||
|
||
## ⚒ ⚫️ ✔
|
||
|
||
🕐❔ 👥 🚫 💪 📣 🌅 🔬 ⚖️ 🗃, 👥 💪 ⚒ `q` 🔢 🔢 ✔ 🚫 📣 🔢 💲, 💖:
|
||
|
||
```Python
|
||
q: str
|
||
```
|
||
|
||
↩️:
|
||
|
||
```Python
|
||
q: Union[str, None] = None
|
||
```
|
||
|
||
✋️ 👥 🔜 📣 ⚫️ ⏮️ `Query`, 🖼 💖:
|
||
|
||
```Python
|
||
q: Union[str, None] = Query(default=None, min_length=3)
|
||
```
|
||
|
||
, 🕐❔ 👆 💪 📣 💲 ✔ ⏪ ⚙️ `Query`, 👆 💪 🎯 🚫 📣 🔢 💲:
|
||
|
||
```Python hl_lines="7"
|
||
{!../../../docs_src/query_params_str_validations/tutorial006.py!}
|
||
```
|
||
|
||
### ✔ ⏮️ ❕ (`...`)
|
||
|
||
📤 🎛 🌌 🎯 📣 👈 💲 ✔. 👆 💪 ⚒ `default` 🔢 🔑 💲 `...`:
|
||
|
||
```Python hl_lines="7"
|
||
{!../../../docs_src/query_params_str_validations/tutorial006b.py!}
|
||
```
|
||
|
||
!!! info
|
||
🚥 👆 🚫 👀 👈 `...` ⏭: ⚫️ 🎁 👁 💲, ⚫️ <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">🍕 🐍 & 🤙 "❕"</a>.
|
||
|
||
⚫️ ⚙️ 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 🩺 🔃 <a href="https://pydantic-docs.helpmanual.io/usage/models/#required-optional-fields" class="external-link" target="_blank">✔ 📦 🏑</a>.
|
||
|
||
### ⚙️ Pydantic `Required` ↩️ ❕ (`...`)
|
||
|
||
🚥 👆 💭 😬 ⚙️ `...`, 👆 💪 🗄 & ⚙️ `Required` ⚪️➡️ Pydantic:
|
||
|
||
```Python hl_lines="2 8"
|
||
{!../../../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`.
|
||
|
||
, 📨 👈 📛 🔜:
|
||
|
||
```JSON
|
||
{
|
||
"q": [
|
||
"foo",
|
||
"bar"
|
||
]
|
||
}
|
||
```
|
||
|
||
!!! tip
|
||
📣 🔢 🔢 ⏮️ 🆎 `list`, 💖 🖼 🔛, 👆 💪 🎯 ⚙️ `Query`, ⏪ ⚫️ 🔜 🔬 📨 💪.
|
||
|
||
🎓 🛠️ 🩺 🔜 ℹ ➡️, ✔ 💗 💲:
|
||
|
||
<img src="/img/tutorial/query-params-str-validations/image02.png">
|
||
|
||
### 🔢 🔢 📇 / 💗 💲 ⏮️ 🔢
|
||
|
||
& 👆 💪 🔬 🔢 `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"]` & 👆 📨 🔜:
|
||
|
||
```JSON
|
||
{
|
||
"q": [
|
||
"foo",
|
||
"bar"
|
||
]
|
||
}
|
||
```
|
||
|
||
#### ⚙️ `list`
|
||
|
||
👆 💪 ⚙️ `list` 🔗 ↩️ `List[str]` (⚖️ `list[str]` 🐍 3️⃣.9️⃣ ➕):
|
||
|
||
```Python hl_lines="7"
|
||
{!../../../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!}
|
||
```
|
||
|
||
## 😛 🔢
|
||
|
||
🔜 ➡️ 💬 👆 🚫 💖 👉 🔢 🚫🔜.
|
||
|
||
👆 ✔️ 👈 ⚫️ 📤 ⏪ ↩️ 📤 👩💻 ⚙️ ⚫️, ✋️ 👆 💚 🩺 🎯 🎦 ⚫️ <abbr title="obsolete, recommended not to use it">😢</abbr>.
|
||
|
||
⤴️ 🚶♀️ 🔢 `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!}
|
||
```
|
||
|
||
🩺 🔜 🎦 ⚫️ 💖 👉:
|
||
|
||
<img src="/img/tutorial/query-params-str-validations/image01.png">
|
||
|
||
## 🚫 ⚪️➡️ 🗄
|
||
|
||
🚫 🔢 🔢 ⚪️➡️ 🏗 🗄 🔗 (& ➡️, ⚪️➡️ 🏧 🧾 ⚙️), ⚒ 🔢 `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` 💲.
|
||
|
||
👀 ⏭ 📃 👀 ❔ 📣 🔬 🎏 🆎, 💖 🔢.
|