-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ Add Korean translation for `docs/ko/docs/tutorial/body-nested-modelโฆ
โฆs.md` (#2506)
- Loading branch information
1 parent
896f171
commit 01d774d
Showing
1 changed file
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
# ๋ณธ๋ฌธ - ์ค์ฒฉ ๋ชจ๋ธ | ||
|
||
**FastAPI**๋ฅผ ์ด์ฉํ๋ฉด (Pydantic ๋๋ถ์) ๋จ๋ ์ผ๋ก ๊น์ด ์ค์ฒฉ๋ ๋ชจ๋ธ์ ์ ์, ๊ฒ์ฆ, ๋ฌธ์ํํ๋ฉฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. | ||
## ๋ฆฌ์คํธ ํ๋ | ||
|
||
์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์๋ธํ์ ์ผ๋ก ์ ์ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ํ์ด์ฌ `list`๋: | ||
|
||
```Python hl_lines="14" | ||
{!../../../docs_src/body_nested_models/tutorial001.py!} | ||
``` | ||
|
||
์ด๋ `tags`๋ฅผ ํญ๋ชฉ ๋ฆฌ์คํธ๋ก ๋ง๋ญ๋๋ค. ๊ฐ ํญ๋ชฉ์ ํ์ ์ ์ ์ธํ์ง ์๋๋ผ๋์. | ||
|
||
## ํ์ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋ฆฌ์คํธ ํ๋ | ||
|
||
ํ์ง๋ง ํ์ด์ฌ์ ๋ด๋ถ์ ํ์ ์ด๋ "ํ์ ๋งค๊ฐ๋ณ์"๋ฅผ ์ ์ธํ ์ ์๋ ํน์ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค: | ||
|
||
### typing์ `List` ์ํฌํธ | ||
|
||
๋จผ์ , ํ์ด์ฌ ํ์ค `typing` ๋ชจ๋์์ `List`๋ฅผ ์ํฌํธํฉ๋๋ค: | ||
|
||
```Python hl_lines="1" | ||
{!../../../docs_src/body_nested_models/tutorial002.py!} | ||
``` | ||
|
||
### ํ์ ๋งค๊ฐ๋ณ์๋ก `List` ์ ์ธ | ||
|
||
`list`, `dict`, `tuple`๊ณผ ๊ฐ์ ํ์ ๋งค๊ฐ๋ณ์(๋ด๋ถ ํ์ )๋ฅผ ๊ฐ๋ ํ์ ์ ์ ์ธํ๋ ค๋ฉด: | ||
|
||
* `typing` ๋ชจ๋์์ ์ํฌํธ | ||
* ๋๊ดํธ๋ฅผ ์ฌ์ฉํ์ฌ "ํ์ ๋งค๊ฐ๋ณ์"๋ก ๋ด๋ถ ํ์ ์ ๋ฌ: `[` ๋ฐ `]` | ||
|
||
```Python | ||
from typing import List | ||
|
||
my_list: List[str] | ||
``` | ||
|
||
์ด ๋ชจ๋ ๊ฒ์ ํ์ ์ ์ธ์ ์ํ ํ์ค ํ์ด์ฌ ๋ฌธ๋ฒ์ ๋๋ค. | ||
|
||
๋ด๋ถ ํ์ ์ ๊ฐ๋ ๋ชจ๋ธ ์ดํธ๋ฆฌ๋ทฐํธ์ ๋ํด ๋์ผํ ํ์ค ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ธ์. | ||
|
||
๋ง์ฐฌ๊ฐ์ง๋ก ์์ ์์ `tags`๋ฅผ ๊ตฌ์ฒด์ ์ผ๋ก "๋ฌธ์์ด์ ๋ฆฌ์คํธ"๋ก ๋ง๋ค ์ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="14" | ||
{!../../../docs_src/body_nested_models/tutorial002.py!} | ||
``` | ||
|
||
## ์งํฉ ํ์ | ||
|
||
๊ทธ๋ฐ๋ฐ ์๊ฐํด๋ณด๋ ํ๊ทธ๋ ๋ฐ๋ณต๋๋ฉด ์ ๋ผ๊ณ , ๊ณ ์ ํ(Unique) ๋ฌธ์์ด์ด์ด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. | ||
|
||
๊ทธ๋ฆฌ๊ณ ํ์ด์ฌ์ ์งํฉ์ ์ํ ํน๋ณํ ๋ฐ์ดํฐ ํ์ `set`์ด ์์ต๋๋ค. | ||
|
||
๊ทธ๋ ๋ค๋ฉด `Set`์ ์ํฌํธ ํ๊ณ `tags`๋ฅผ `str`์ `set`์ผ๋ก ์ ์ธํ ์ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="1 14" | ||
{!../../../docs_src/body_nested_models/tutorial003.py!} | ||
``` | ||
|
||
๋๋ถ์ ์ค๋ณต ๋ฐ์ดํฐ๊ฐ ์๋ ์์ฒญ์ ์์ ํ๋๋ผ๋ ๊ณ ์ ํ ํญ๋ชฉ๋ค์ ์งํฉ์ผ๋ก ๋ณํ๋ฉ๋๋ค. | ||
|
||
๊ทธ๋ฆฌ๊ณ ํด๋น ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅ ํ ๋๋ง๋ค ์์ค์ ์ค๋ณต์ด ์๋๋ผ๋ ๊ณ ์ ํ ํญ๋ชฉ๋ค์ ์งํฉ์ผ๋ก ์ถ๋ ฅ๋ฉ๋๋ค. | ||
|
||
๋ํ ๊ทธ์ ๋ฐ๋ผ ์ฃผ์์ด ์๊ธฐ๊ณ ๋ฌธ์ํ๋ฉ๋๋ค. | ||
|
||
## ์ค์ฒฉ ๋ชจ๋ธ | ||
|
||
Pydantic ๋ชจ๋ธ์ ๊ฐ ์ดํธ๋ฆฌ๋ทฐํธ๋ ํ์ ์ ๊ฐ์ต๋๋ค. | ||
|
||
๊ทธ๋ฐ๋ฐ ํด๋น ํ์ ์์ฒด๋ก ๋๋ค๋ฅธ Pydantic ๋ชจ๋ธ์ ํ์ ์ด ๋ ์ ์์ต๋๋ค. | ||
|
||
๊ทธ๋ฌ๋ฏ๋ก ํน์ ํ ์ดํธ๋ฆฌ๋ทฐํธ์ ์ด๋ฆ, ํ์ , ๊ฒ์ฆ์ ์ฌ์ฉํ์ฌ ๊น๊ฒ ์ค์ฒฉ๋ JSON "๊ฐ์ฒด"๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค. | ||
|
||
๋ชจ๋ ๊ฒ์ด ๋จ๋ ์ผ๋ก ์ค์ฒฉ๋ฉ๋๋ค. | ||
|
||
### ์๋ธ๋ชจ๋ธ ์ ์ | ||
|
||
์๋ฅผ ๋ค์ด, `Image` ๋ชจ๋ธ์ ์ ์ธํ ์ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="9-11" | ||
{!../../../docs_src/body_nested_models/tutorial004.py!} | ||
``` | ||
|
||
### ์๋ธ๋ชจ๋์ ํ์ ์ผ๋ก ์ฌ์ฉ | ||
|
||
๊ทธ๋ฆฌ๊ณ ์ดํธ๋ฆฌ๋ทฐํธ์ ํ์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="20" | ||
{!../../../docs_src/body_nested_models/tutorial004.py!} | ||
``` | ||
|
||
์ด๋ **FastAPI**๊ฐ ๋ค์๊ณผ ์ ์ฌํ ๋ณธ๋ฌธ์ ๊ธฐ๋ํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค: | ||
|
||
```JSON | ||
{ | ||
"name": "Foo", | ||
"description": "The pretender", | ||
"price": 42.0, | ||
"tax": 3.2, | ||
"tags": ["rock", "metal", "bar"], | ||
"image": { | ||
"url": "http://example.com/baz.jpg", | ||
"name": "The Foo live" | ||
} | ||
} | ||
``` | ||
|
||
๋ค์ ํ๋ฒ, **FastAPI**๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์ ์ธ์ ํจ์ผ๋ก์จ ์ป๋ ๊ฒ์: | ||
|
||
* ์ค์ฒฉ ๋ชจ๋ธ๋ ํธ์ง๊ธฐ ์ง์(์๋์์ฑ ๋ฑ) | ||
* ๋ฐ์ดํฐ ๋ณํ | ||
* ๋ฐ์ดํฐ ๊ฒ์ฆ | ||
* ์๋ ๋ฌธ์ํ | ||
|
||
## ํน๋ณํ ํ์ ๊ณผ ๊ฒ์ฆ | ||
|
||
`str`, `int`, `float` ๋ฑ๊ณผ ๊ฐ์ ๋จ์ผ ํ์ ๊ณผ๋ ๋ณ๊ฐ๋ก, `str`์ ์์ํ๋ ๋ ๋ณต์กํ ๋จ์ผ ํ์ ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. | ||
|
||
๋ชจ๋ ์ต์ ์ ๋ณด๋ ค๋ฉด, <a href="https://pydantic-docs.helpmanual.io/usage/types/" class="external-link" target="_blank">Pydantic's exotic types</a> ๋ฌธ์๋ฅผ ํ์ธํ์ธ์. ๋ค์ ์ฅ์์ ๋ช๊ฐ์ง ์์ ๋ฅผ ๋ณผ ์ ์์ต๋๋ค. | ||
|
||
์๋ฅผ ๋ค์ด `Image` ๋ชจ๋ธ ์์ `url` ํ๋๋ฅผ `str` ๋์ Pydantic์ `HttpUrl`๋ก ์ ์ธํ ์ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="4 10" | ||
{!../../../docs_src/body_nested_models/tutorial005.py!} | ||
``` | ||
|
||
์ด ๋ฌธ์์ด์ด ์ ํจํ URL์ธ์ง ๊ฒ์ฌํ๊ณ JSON ์คํค๋ง/OpenAPI๋ก ๋ฌธ์ํ ๋ฉ๋๋ค. | ||
|
||
## ์๋ธ๋ชจ๋ธ ๋ฆฌ์คํธ๋ฅผ ๊ฐ๋ ์ดํธ๋ฆฌ๋ทฐํธ | ||
|
||
`list`, `set` ๋ฑ์ ์๋ธํ์ ์ผ๋ก Pydantic ๋ชจ๋ธ์ ์ฌ์ฉํ ์๋ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="20" | ||
{!../../../docs_src/body_nested_models/tutorial006.py!} | ||
``` | ||
|
||
์๋์ ๊ฐ์ JSON ๋ณธ๋ฌธ์ผ๋ก ์์(๋ณํ, ๊ฒ์ฆ, ๋ฌธ์ํ ๋ฑ์)ํฉ๋๋ค: | ||
|
||
```JSON hl_lines="11" | ||
{ | ||
"name": "Foo", | ||
"description": "The pretender", | ||
"price": 42.0, | ||
"tax": 3.2, | ||
"tags": [ | ||
"rock", | ||
"metal", | ||
"bar" | ||
], | ||
"images": [ | ||
{ | ||
"url": "http://example.com/baz.jpg", | ||
"name": "The Foo live" | ||
}, | ||
{ | ||
"url": "http://example.com/dave.jpg", | ||
"name": "The Baz" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
!!! info "์ ๋ณด" | ||
`images` ํค๊ฐ ์ด๋ป๊ฒ ์ด๋ฏธ์ง ๊ฐ์ฒด ๋ฆฌ์คํธ๋ฅผ ๊ฐ๋์ง ์ฃผ๋ชฉํ์ธ์. | ||
|
||
## ๊น๊ฒ ์ค์ฒฉ๋ ๋ชจ๋ธ | ||
|
||
๋จ๋ ์ผ๋ก ๊น๊ฒ ์ค์ฒฉ๋ ๋ชจ๋ธ์ ์ ์ํ ์ ์์ต๋๋ค: | ||
|
||
```Python hl_lines="9 14 20 23 27" | ||
{!../../../docs_src/body_nested_models/tutorial007.py!} | ||
``` | ||
|
||
!!! info "์ ๋ณด" | ||
`Offer`๊ฐ ์ ํ์ฌํญ `Image` ๋ฆฌ์คํธ๋ฅผ ์ฐจ๋ก๋ก ๊ฐ๋ `Item` ๋ฆฌ์คํธ๋ฅผ ์ด๋ป๊ฒ ๊ฐ์ง๊ณ ์๋์ง ์ฃผ๋ชฉํ์ธ์ | ||
|
||
## ์์ ๋ฆฌ์คํธ์ ๋ณธ๋ฌธ | ||
|
||
์์๋๋ JSON ๋ณธ๋ฌธ์ ์ต์์ ๊ฐ์ด JSON `array`(ํ์ด์ฌ `list`)๋ฉด, Pydantic ๋ชจ๋ธ์์์ ๋ง์ฐฌ๊ฐ์ง๋ก ํจ์์ ๋งค๊ฐ๋ณ์์์ ํ์ ์ ์ ์ธํ ์ ์์ต๋๋ค: | ||
|
||
```Python | ||
images: List[Image] | ||
``` | ||
|
||
์ด๋ฅผ ์๋์ฒ๋ผ: | ||
|
||
```Python hl_lines="15" | ||
{!../../../docs_src/body_nested_models/tutorial008.py!} | ||
``` | ||
|
||
## ์ด๋์๋ ํธ์ง๊ธฐ ์ง์ | ||
|
||
๊ทธ๋ฆฌ๊ณ ์ด๋์๋ ํธ์ง๊ธฐ ์ง์์ ๋ฐ์์ ์์ต๋๋ค. | ||
|
||
๋ฆฌ์คํธ ๋ด๋ถ ํญ๋ชฉ์ ๊ฒฝ์ฐ์๋: | ||
|
||
<img src="/img/tutorial/body-nested-models/image01.png"> | ||
|
||
Pydantic ๋ชจ๋ธ ๋์ ์ `dict`๋ฅผ ์ง์ ์ฌ์ฉํ์ฌ ์์ ํ ๊ฒฝ์ฐ, ์ด๋ฌํ ํธ์ง๊ธฐ ์ง์์ ๋ฐ์์ ์์ต๋๋ค. | ||
|
||
ํ์ง๋ง ์์ ํ ๋์ ๋๋ฆฌ๊ฐ ์๋์ผ๋ก ๋ณํ๋๊ณ ์ถ๋ ฅ๋ ์๋์ผ๋ก JSON์ผ๋ก ๋ณํ๋๋ฏ๋ก ๊ฑฑ์ ํ ํ์๋ ์์ต๋๋ค. | ||
|
||
## ๋จ๋ `dict`์ ๋ณธ๋ฌธ | ||
|
||
์ผ๋ถ ํ์ ์ ํค์ ๋ค๋ฅธ ํ์ ์ ๊ฐ์ ์ฌ์ฉํ์ฌ `dict`๋ก ๋ณธ๋ฌธ์ ์ ์ธํ ์ ์์ต๋๋ค. | ||
|
||
(Pydantic์ ์ฌ์ฉํ ๊ฒฝ์ฐ์ฒ๋ผ) ์ ํจํ ํ๋/์ดํธ๋ฆฌ๋ทฐํธ ์ด๋ฆ์ด ๋ฌด์์ธ์ง ์ ํ์๊ฐ ์์ต๋๋ค. | ||
|
||
์์ง ๋ชจ๋ฅด๋ ํค๋ฅผ ๋ฐ์ผ๋ ค๋ ๊ฒฝ์ฐ ์ ์ฉํฉ๋๋ค. | ||
|
||
--- | ||
|
||
๋ค๋ฅธ ์ ์ฉํ ๊ฒฝ์ฐ๋ ๋ค๋ฅธ ํ์ ์ ํค๋ฅผ ๊ฐ์ง ๋์ ๋๋ค. ์. `int`. | ||
|
||
์ฌ๊ธฐ์ ๊ทธ ๊ฒฝ์ฐ๋ฅผ ๋ณผ ๊ฒ์ ๋๋ค. | ||
|
||
์ด ๊ฒฝ์ฐ, `float` ๊ฐ์ ๊ฐ์ง `int` ํค๊ฐ ์๋ ๋ชจ๋ `dict`๋ฅผ ๋ฐ์๋ค์ ๋๋ค: | ||
|
||
```Python hl_lines="15" | ||
{!../../../docs_src/body_nested_models/tutorial009.py!} | ||
``` | ||
|
||
!!! tip "ํ" | ||
JSON์ ์ค์ง `str`ํ ํค๋ง ์ง์ํ๋ค๋ ๊ฒ์ ์ผ๋์ ๋์ธ์. | ||
|
||
ํ์ง๋ง Pydantic์ ์๋ ๋ฐ์ดํฐ ๋ณํ์ด ์์ต๋๋ค. | ||
|
||
์ฆ, API ํด๋ผ์ด์ธํธ๊ฐ ๋ฌธ์์ด์ ํค๋ก ๋ณด๋ด๋๋ผ๋ ํด๋น ๋ฌธ์์ด์ด ์์ํ ์ ์๋ฅผ ํฌํจํ๋ํ Pydantic์ ์ด๋ฅผ ๋ณํํ๊ณ ๊ฒ์ฆํฉ๋๋ค. | ||
|
||
๊ทธ๋ฌ๋ฏ๋ก `weights`๋ก ๋ฐ์ `dict`๋ ์ค์ ๋ก `int` ํค์ `float` ๊ฐ์ ๊ฐ์ง๋๋ค. | ||
|
||
## ์์ฝ | ||
|
||
**FastAPI**๋ฅผ ์ฌ์ฉํ๋ฉด Pydantic ๋ชจ๋ธ์ด ์ ๊ณตํ๋ ์ต๋ ์ ์ฐ์ฑ์ ํ๋ณดํ๋ฉด์ ์ฝ๋๋ฅผ ๊ฐ๋จํ๊ณ ์งง๊ฒ, ๊ทธ๋ฆฌ๊ณ ์ฐ์ํ๊ฒ ์ ์งํ ์ ์์ต๋๋ค. | ||
|
||
๋ฌผ๋ก ์๋์ ์ด์ ๋ ์์ต๋๋ค: | ||
|
||
* ํธ์ง๊ธฐ ์ง์ (์๋์์ฑ์ด ์ด๋์๋!) | ||
* ๋ฐ์ดํฐ ๋ณํ (์ผ๋ช ํ์ฑ/์ง๋ ฌํ) | ||
* ๋ฐ์ดํฐ ๊ฒ์ฆ | ||
* ์คํค๋ง ๋ฌธ์ํ | ||
* ์๋ ๋ฌธ์ |