committed by
GitHub
1 changed files with 116 additions and 0 deletions
@ -0,0 +1,116 @@ |
|||||
|
# ๋ณธ๋ฌธ - ํ๋ |
||||
|
|
||||
|
`Query`, `Path`์ `Body`๋ฅผ ์ฌ์ฉํด *๊ฒฝ๋ก ๋์ ํจ์* ๋งค๊ฐ๋ณ์ ๋ด์์ ์ถ๊ฐ์ ์ธ ๊ฒ์ฆ์ด๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ ์ธํ ๊ฒ์ฒ๋ผ Pydantic์ `Field`๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ ๋ด์์ ๊ฒ์ฆ๊ณผ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
## `Field` ์ํฌํธ |
||||
|
|
||||
|
๋จผ์ ์ด๋ฅผ ์ํฌํธํด์ผ ํฉ๋๋ค: |
||||
|
|
||||
|
=== "Python 3.10+" |
||||
|
|
||||
|
```Python hl_lines="4" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_an_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.9+" |
||||
|
|
||||
|
```Python hl_lines="4" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_an_py39.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.8+" |
||||
|
|
||||
|
```Python hl_lines="4" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_an.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.10+ Annotated๊ฐ ์๋ ๊ฒฝ์ฐ" |
||||
|
|
||||
|
!!! ํ |
||||
|
๊ฐ๋ฅํ๋ค๋ฉด `Annotated`๊ฐ ๋ฌ๋ฆฐ ๋ฒ์ ์ ๊ถ์ฅํฉ๋๋ค. |
||||
|
|
||||
|
```Python hl_lines="2" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.8+ Annotated๊ฐ ์๋ ๊ฒฝ์ฐ" |
||||
|
|
||||
|
!!! tip "ํ" |
||||
|
๊ฐ๋ฅํ๋ค๋ฉด `Annotated`๊ฐ ๋ฌ๋ฆฐ ๋ฒ์ ์ ๊ถ์ฅํฉ๋๋ค. |
||||
|
|
||||
|
```Python hl_lines="4" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001.py!} |
||||
|
``` |
||||
|
|
||||
|
!!! warning "๊ฒฝ๊ณ " |
||||
|
`Field`๋ ๋ค๋ฅธ ๊ฒ๋ค์ฒ๋ผ (`Query`, `Path`, `Body` ๋ฑ) `fastapi`์์๊ฐ ์๋ `pydantic`์์ ๋ฐ๋ก ์ํฌํธ ๋๋ ์ ์ ์ฃผ์ํ์ธ์. |
||||
|
|
||||
|
## ๋ชจ๋ธ ์ดํธ๋ฆฌ๋ทฐํธ ์ ์ธ |
||||
|
|
||||
|
๊ทธ ๋ค์ ๋ชจ๋ธ ์ดํธ๋ฆฌ๋ทฐํธ์ ํจ๊ป `Field`๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
=== "Python 3.10+" |
||||
|
|
||||
|
```Python hl_lines="11-14" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_an_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.9+" |
||||
|
|
||||
|
```Python hl_lines="11-14" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_an_py39.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.8+" |
||||
|
|
||||
|
```Python hl_lines="12-15" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_an.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.10+ Annotated๊ฐ ์๋ ๊ฒฝ์ฐ" |
||||
|
|
||||
|
!!! tip "ํ" |
||||
|
๊ฐ๋ฅํ๋ค๋ฉด `Annotated`๊ฐ ๋ฌ๋ฆฐ ๋ฒ์ ์ ๊ถ์ฅํฉ๋๋ค. |
||||
|
|
||||
|
```Python hl_lines="9-12" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001_py310.py!} |
||||
|
``` |
||||
|
|
||||
|
=== "Python 3.8+ Annotated๊ฐ ์๋ ๊ฒฝ์ฐ" |
||||
|
|
||||
|
!!! tip "ํ" |
||||
|
๊ฐ๋ฅํ๋ค๋ฉด `Annotated`๊ฐ ๋ฌ๋ฆฐ ๋ฒ์ ์ ๊ถ์ฅํฉ๋๋ค. |
||||
|
|
||||
|
```Python hl_lines="11-14" |
||||
|
{!> ../../../docs_src/body_fields/tutorial001.py!} |
||||
|
``` |
||||
|
|
||||
|
`Field`๋ `Query`, `Path`์ `Body`์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋์ํ๋ฉฐ, ๋ชจ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ค ๋ฑ์ ๊ฐ์ง๋๋ค. |
||||
|
|
||||
|
!!! note "๊ธฐ์ ์ ์ธ๋ถ์ฌํญ" |
||||
|
์ค์ ๋ก `Query`, `Path`๋ฑ, ์ฌ๋ฌ๋ถ์ด ์์ผ๋ก ๋ณผ ๋ค๋ฅธ ๊ฒ๋ค์ ๊ณตํต ํด๋์ค์ธ `Param` ํด๋์ค์ ์๋ธํด๋์ค ๊ฐ์ฒด๋ฅผ ๋ง๋๋๋ฐ, ๊ทธ ์์ฒด๋ก Pydantic์ `FieldInfo` ํด๋์ค์ ์๋ธํด๋์ค์
๋๋ค. |
||||
|
|
||||
|
๊ทธ๋ฆฌ๊ณ Pydantic์ `Field` ๋ํ `FieldInfo`์ ์ธ์คํด์ค๋ฅผ ๋ฐํํฉ๋๋ค. |
||||
|
|
||||
|
`Body` ๋ํ `FieldInfo`์ ์๋ธํด๋์ค ๊ฐ์ฒด๋ฅผ ์ง์ ์ ์ผ๋ก ๋ฐํํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ `Body` ํด๋์ค์ ์๋ธํด๋์ค์ธ ๊ฒ๋ค๋ ์ฌ๋ฌ๋ถ์ด ๋์ค์ ๋ณด๊ฒ๋ ๊ฒ์
๋๋ค. |
||||
|
|
||||
|
`Query`, `Path`์ ๊ทธ ์ธ ๊ฒ๋ค์ `fastapi`์์ ์ํฌํธํ ๋, ์ด๋ ์ค์ ๋ก ํน๋ณํ ํด๋์ค๋ฅผ ๋ฐํํ๋ ํจ์์ธ ๊ฒ์ ๊ธฐ์ตํด ์ฃผ์ธ์. |
||||
|
|
||||
|
!!! tip "ํ" |
||||
|
์ฃผ๋ชฉํ ์ ์ ํ์
, ๊ธฐ๋ณธ ๊ฐ ๋ฐ `Field`๋ก ์ด๋ฃจ์ด์ง ๊ฐ ๋ชจ๋ธ ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ `Path`, `Query`์ `Body`๋์ `Field`๋ฅผ ์ฌ์ฉํ๋ *๊ฒฝ๋ก ์๋ ํจ์*์ ๋งค๊ฐ๋ณ์์ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค๋ ์ ์
๋๋ค. |
||||
|
|
||||
|
## ๋ณ๋ ์ ๋ณด ์ถ๊ฐ |
||||
|
|
||||
|
`Field`, `Query`, `Body`, ๊ทธ ์ธ ์์ ๋ณ๋ ์ ๋ณด๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค. ์ด๋ ์์ฑ๋ JSON ์คํค๋ง์ ํฌํจ๋ฉ๋๋ค. |
||||
|
|
||||
|
์ฌ๋ฌ๋ถ์ด ์์ ๋ฅผ ์ ์ธํ ๋ ๋์ค์ ์ด ๊ณต์ ๋ฌธ์์์ ๋ณ๋ ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ธ ๊ฒ์
๋๋ค. |
||||
|
|
||||
|
!!! warning "๊ฒฝ๊ณ " |
||||
|
๋ณ๋ ํค๊ฐ ์ ๋ฌ๋ `Field` ๋ํ ์ฌ๋ฌ๋ถ์ ์ดํ๋ฆฌ์ผ์ด์
์ OpenAPI ์คํค๋ง์ ๋ํ๋ ๊ฒ์
๋๋ค. |
||||
|
์ด๋ฐ ํค๊ฐ OpenAPI ๋ช
์ธ์, [the OpenAPI validator](https://validator.swagger.io/)๊ฐ์ ๋ช๋ช OpenAPI ๋๊ตฌ๋ค์ ํฌํจ๋์ง ๋ชปํ ์ ์์ผ๋ฉฐ, ์ฌ๋ฌ๋ถ์ด ์์ฑํ ์คํค๋ง์ ํธํ๋์ง ์์ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
## ์์ฝ |
||||
|
|
||||
|
๋ชจ๋ธ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ํ ์ถ๊ฐ ๊ฒ์ฆ๊ณผ ๋ฉํ๋ฐ์ดํฐ ์ ์ธํ๊ธฐ ์ํด Pydantic์ `Field` ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
๋ํ ์ถ๊ฐ์ ์ธ JSON ์คํค๋ง ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ธฐ ์ํ ๋ณ๋์ ํค์๋ ์ธ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. |
Loadingโฆ
Reference in new issue