# โ ๐ท
โถ๏ธ โฎ๏ธ โฎ๏ธ ๐ผ, โซ๏ธ ๐ โ โ๏ธ ๐
๐ 1๏ธโฃ ๐ ๐ท.
๐ โด๏ธ ๐ผ ๐ฉโ๐ป ๐ท, โฉ๏ธ:
* **๐ข ๐ท** ๐ช ๐ช โ๏ธ ๐.
* **๐ข ๐ท** ๐ ๐ซ โ๏ธ ๐.
* **๐ฝ ๐ท** ๐ ๐ฒ ๐ช โ๏ธ #๏ธโฃ ๐.
!!! danger
๐
๐ช ๐ฉโ๐ป ๐ข ๐. ๐ง ๐ช "๐ #๏ธโฃ" ๐ ๐ ๐ช โคด๏ธ โ.
๐ฅ ๐ ๐ซ ๐ญ, ๐ ๐ ๐ก โซ๏ธโ "๐#๏ธโฃ" [๐โโ ๐](security/simple-oauth2.md#password-hashing){.internal-link target=_blank}.
## ๐ ๐ท
๐ฅ ๐ข ๐ญ โ ๐ท ๐ช ๐ ๐ โฎ๏ธ ๐ซ ๐ ๐ & ๐ฅ ๐โ ๐ซ โ๏ธ:
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐"
```Python hl_lines="9 11 16 22 24 29-30 33-35 40-41"
{!> ../../../docs_src/extra_models/tutorial001.py!}
```
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐"
```Python hl_lines="7 9 14 20 22 27-28 31-33 38-39"
{!> ../../../docs_src/extra_models/tutorial001_py310.py!}
```
### ๐ `**user_in.dict()`
#### Pydantic `.dict()`
`user_in` Pydantic ๐ท ๐ `UserIn`.
Pydantic ๐ท โ๏ธ `.dict()` ๐ฉโ๐ฌ ๐ ๐จ `dict` โฎ๏ธ ๐ท ๐ฝ.
, ๐ฅ ๐ฅ โ Pydantic ๐ `user_in` ๐:
```Python
user_in = UserIn(username="john", password="secret", email="john.doe@example.com")
```
& โคด๏ธ ๐ฅ ๐ค:
```Python
user_dict = user_in.dict()
```
๐ฅ ๐ โ๏ธ `dict` โฎ๏ธ ๐ฝ ๐ข `user_dict` (โซ๏ธ `dict` โฉ๏ธ Pydantic ๐ท ๐).
& ๐ฅ ๐ฅ ๐ค:
```Python
print(user_dict)
```
๐ฅ ๐ ๐ค ๐ `dict` โฎ๏ธ:
```Python
{
'username': 'john',
'password': 'secret',
'email': 'john.doe@example.com',
'full_name': None,
}
```
#### ๐ `dict`
๐ฅ ๐ฅ โ `dict` ๐ `user_dict` & ๐ถโโ๏ธ โซ๏ธ ๐ข (โ๏ธ ๐) โฎ๏ธ `**user_dict`, ๐ ๐ "๐" โซ๏ธ. โซ๏ธ ๐ ๐ถโโ๏ธ ๐ & ๐ฒ `user_dict` ๐ ๐-๐ฒ โ.
, โถ๏ธ โฎ๏ธ `user_dict` โช๏ธโก๏ธ ๐, โ:
```Python
UserInDB(**user_dict)
```
๐ ๐ ๐ณ ๐:
```Python
UserInDB(
username="john",
password="secret",
email="john.doe@example.com",
full_name=None,
)
```
โ๏ธ ๐
โซ๏ธโ, โ๏ธ `user_dict` ๐, โฎ๏ธ โซ๏ธโ ๐ โซ๏ธ ๐ช โ๏ธ ๐ฎ:
```Python
UserInDB(
username = user_dict["username"],
password = user_dict["password"],
email = user_dict["email"],
full_name = user_dict["full_name"],
)
```
#### Pydantic ๐ท โช๏ธโก๏ธ ๐ โ1๏ธโฃ
๐ผ ๐ ๐ฅ ๐ค `user_dict` โช๏ธโก๏ธ `user_in.dict()`, ๐ ๐:
```Python
user_dict = user_in.dict()
UserInDB(**user_dict)
```
๐ ๐:
```Python
UserInDB(**user_in.dict())
```
...โฉ๏ธ `user_in.dict()` `dict`, & โคด๏ธ ๐ฅ โ ๐ "๐" โซ๏ธ ๐ถโโ๏ธ โซ๏ธ `UserInDB` ๐ โฎ๏ธ `**`.
, ๐ฅ ๐ค Pydantic ๐ท โช๏ธโก๏ธ ๐ฝ โ1๏ธโฃ Pydantic ๐ท.
#### ๐ `dict` & โ ๐จ๐ป
& โคด๏ธ โ โ ๐จ๐ป โ `hashed_password=hashed_password`, ๐:
```Python
UserInDB(**user_in.dict(), hashed_password=hashed_password)
```
...๐ ๐ ๐โโ ๐:
```Python
UserInDB(
username = user_dict["username"],
password = user_dict["password"],
email = user_dict["email"],
full_name = user_dict["full_name"],
hashed_password = hashed_password,
)
```
!!! warning
๐ ๐ ๐ข ๐ค ๐ช ๐ง ๐ฝ, โ๏ธ ๐ซ โ๏ธ ๐ซ ๐ ๐ ๐ฐ ๐โโ.
## ๐ โ
๐ ๐ โ 1๏ธโฃ ๐ ๐ญ **FastAPI**.
๐ โ ๐ ๐ค ๐, ๐โโ โ, ๐ ๐ โ (๐โ ๐ โน 1๏ธโฃ ๐ฅ โ๏ธ ๐ซ ๐), โ๏ธ.
& ๐ ๐ท ๐ ๐ค ๐ ๐ฝ & โ ๐ข ๐ & ๐.
๐ฅ ๐ช ๐ป.
๐ฅ ๐ช ๐ฃ `UserBase` ๐ท ๐ ๐ฆ ๐งข ๐ ๐ ๐ท. & โคด๏ธ ๐ฅ ๐ช โ ๐ฟ ๐ ๐ท ๐ ๐ ๐ฎ ๐ข (๐ ๐, ๐ฌ, โ๏ธ).
๐ ๐ฝ ๐ ๏ธ, ๐ฌ, ๐งพ, โ๏ธ. ๐ ๐ท ๐.
๐ ๐, ๐ฅ ๐ช ๐ฃ ๐บ ๐ ๐ท (โฎ๏ธ ๐ข `password`, โฎ๏ธ `hashed_password` & ๐ต ๐):
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐"
```Python hl_lines="9 15-16 19-20 23-24"
{!> ../../../docs_src/extra_models/tutorial002.py!}
```
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐"
```Python hl_lines="7 13-14 17-18 21-22"
{!> ../../../docs_src/extra_models/tutorial002_py310.py!}
```
## `Union` โ๏ธ `anyOf`
๐ ๐ช ๐ฃ ๐จ `Union` 2๏ธโฃ ๐, ๐ โ, ๐ ๐จ ๐ ๐ 2๏ธโฃ.
โซ๏ธ ๐ ๐ฌ ๐ โฎ๏ธ `anyOf`.
๐, โ๏ธ ๐ฉ ๐ ๐ ๐ `typing.Union`:
!!! note
๐โ โ `Union`, ๐ ๐ ๐ฏ ๐ ๐ฅ, โฉ ๐ ๐ฏ ๐. ๐ผ ๐, ๐ ๐ฏ `PlaneItem` ๐ โญ `CarItem` `Union[PlaneItem, CarItem]`.
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐"
```Python hl_lines="1 14-15 18-20 33"
{!> ../../../docs_src/extra_models/tutorial003.py!}
```
=== "๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ & ๐"
```Python hl_lines="1 14-15 18-20 33"
{!> ../../../docs_src/extra_models/tutorial003_py310.py!}
```
### `Union` ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ
๐ ๐ผ ๐ฅ ๐ถโโ๏ธ `Union[PlaneItem, CarItem]` ๐ฒ โ `response_model`.
โฉ๏ธ ๐ฅ ๐ถโโ๏ธ โซ๏ธ **๐ฒ โ** โฉ๏ธ ๐ฎ โซ๏ธ **๐ โ**, ๐ฅ โ๏ธ โ๏ธ `Union` ๐ 3๏ธโฃ.1๏ธโฃ0๏ธโฃ.
๐ฅ โซ๏ธ ๐ โ ๐ฅ ๐ช โ๏ธ โ๏ธ โธ โธ,:
```Python
some_variable: PlaneItem | CarItem
```
โ๏ธ ๐ฅ ๐ฅ ๐ฎ ๐ `response_model=PlaneItem | CarItem` ๐ฅ ๐ ๐ค โ, โฉ๏ธ ๐ ๐ ๐ ๐ญ **โ ๐ ๏ธ** ๐ `PlaneItem` & `CarItem` โฉ๏ธ ๐ฌ ๐ ๐ โ.
## ๐ ๐ท
๐ ๐, ๐ ๐ช ๐ฃ ๐จ ๐ ๐.
๐, โ๏ธ ๐ฉ ๐ `typing.List` (โ๏ธ `list` ๐ 3๏ธโฃ.9๏ธโฃ & ๐):
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐"
```Python hl_lines="1 20"
{!> ../../../docs_src/extra_models/tutorial004.py!}
```
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐"
```Python hl_lines="18"
{!> ../../../docs_src/extra_models/tutorial004_py39.py!}
```
## ๐จ โฎ๏ธ โ `dict`
๐ ๐ช ๐ฃ ๐จ โ๏ธ โ
โ `dict`, ๐ฃ ๐ ๐ & ๐ฒ, ๐ต โ๏ธ Pydantic ๐ท.
๐ โ ๐ฅ ๐ ๐ซ ๐ญ โ ๐/๐ข ๐ (๐ ๐ ๐ช Pydantic ๐ท) โช.
๐ ๐ผ, ๐ ๐ช โ๏ธ `typing.Dict` (โ๏ธ `dict` ๐ 3๏ธโฃ.9๏ธโฃ & ๐):
=== "๐ 3๏ธโฃ.6๏ธโฃ & ๐"
```Python hl_lines="1 8"
{!> ../../../docs_src/extra_models/tutorial005.py!}
```
=== "๐ 3๏ธโฃ.9๏ธโฃ & ๐"
```Python hl_lines="6"
{!> ../../../docs_src/extra_models/tutorial005_py39.py!}
```
## ๐
โ๏ธ ๐ Pydantic ๐ท & ๐ โก ๐ ๐ผ.
๐ ๐ซ ๐ช โ๏ธ ๐ ๐ฝ ๐ท ๐ ๐จโ๐ผ ๐ฅ ๐ ๐จโ๐ผ ๐ ๐ช โ๏ธ ๐ "๐ต๐ธ". ๐ผ โฎ๏ธ ๐ฉโ๐ป "๐จโ๐ผ" โฎ๏ธ ๐ต๐ธ โ
`password`, `password_hash` & ๐
โโ ๐.