committed by
GitHub
1 changed files with 78 additions and 0 deletions
@ -0,0 +1,78 @@ |
|||||
|
# ํผ ๋ชจ๋ธ |
||||
|
|
||||
|
FastAPI์์ **Pydantic ๋ชจ๋ธ**์ ์ด์ฉํ์ฌ **ํผ ํ๋**๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
/// info | ์ ๋ณด |
||||
|
|
||||
|
ํผ(Form)์ ์ฌ์ฉํ๋ ค๋ฉด, ๋จผ์ <a href="https://github.com/Kludex/python-multipart" class="external-link" target="_blank">`python-multipart`</a>๋ฅผ ์ค์นํ์ธ์. |
||||
|
|
||||
|
[๊ฐ์ ํ๊ฒฝ](../virtual-environments.md){.internal-link target=_blank}์ ์์ฑํ๊ณ ํ์ฑํํ ๋ค์, ์๋์ ๊ฐ์ด ์ค์นํ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
```console |
||||
|
$ pip install python-multipart |
||||
|
``` |
||||
|
|
||||
|
/// |
||||
|
|
||||
|
/// note | ์ฐธ๊ณ |
||||
|
|
||||
|
์ด ๊ธฐ๋ฅ์ FastAPI ๋ฒ์ `0.113.0` ์ดํ๋ถํฐ ์ง์๋ฉ๋๋ค. ๐ค |
||||
|
|
||||
|
/// |
||||
|
|
||||
|
## Pydantic ๋ชจ๋ธ์ ์ฌ์ฉํ ํผ |
||||
|
|
||||
|
**ํผ ํ๋**๋ก ๋ฐ๊ณ ์ถ์ ํ๋๋ฅผ **Pydantic ๋ชจ๋ธ**๋ก ์ ์ธํ ๋ค์, ๋งค๊ฐ๋ณ์๋ฅผ `Form`์ผ๋ก ์ ์ธํ๋ฉด ๋ฉ๋๋ค: |
||||
|
|
||||
|
{* ../../docs_src/request_form_models/tutorial001_an_py39.py hl[9:11,15] *} |
||||
|
|
||||
|
**FastAPI**๋ ์์ฒญ์์ ๋ฐ์ **ํผ ๋ฐ์ดํฐ**์์ **๊ฐ ํ๋**์ ๋ํ ๋ฐ์ดํฐ๋ฅผ **์ถ์ถ**ํ๊ณ ์ ์ํ Pydantic ๋ชจ๋ธ์ ์ค๋๋ค. |
||||
|
|
||||
|
## ๋ฌธ์ ํ์ธํ๊ธฐ |
||||
|
|
||||
|
๋ฌธ์ UI `/docs`์์ ํ์ธํ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
<div class="screenshot"> |
||||
|
<img src="/img/tutorial/request-form-models/image01.png"> |
||||
|
</div> |
||||
|
|
||||
|
## ์ถ๊ฐ ํผ ํ๋ ๊ธ์งํ๊ธฐ |
||||
|
|
||||
|
์ผ๋ถ ํน๋ณํ ์ฌ์ฉ ์ฌ๋ก(ํํ์ง๋ ์๊ฒ ์ง๋ง)์์๋ Pydantic ๋ชจ๋ธ์์ ์ ์ํ ํผ ํ๋๋ฅผ **์ ํ**ํ๊ธธ ์ํ ์๋ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ **์ถ๊ฐ** ํ๋๋ฅผ **๊ธ์ง**ํ ์๋ ์์ต๋๋ค. |
||||
|
|
||||
|
/// note | ์ฐธ๊ณ |
||||
|
|
||||
|
์ด ๊ธฐ๋ฅ์ FastAPI ๋ฒ์ `0.114.0` ์ดํ๋ถํฐ ์ง์๋ฉ๋๋ค. ๐ค |
||||
|
|
||||
|
/// |
||||
|
|
||||
|
Pydantic์ ๋ชจ๋ธ ๊ตฌ์ฑ์ ์ฌ์ฉํ์ฌ ์ถ๊ฐ(`extra`) ํ๋๋ฅผ ๊ธ์ง(`forbid`)ํ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
{* ../../docs_src/request_form_models/tutorial002_an_py39.py hl[12] *} |
||||
|
|
||||
|
ํด๋ผ์ด์ธํธ๊ฐ ์ถ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ค๊ณ ํ๋ฉด **์ค๋ฅ** ์๋ต์ ๋ฐ๊ฒ ๋ฉ๋๋ค. |
||||
|
|
||||
|
์๋ฅผ ๋ค์ด, ํด๋ผ์ด์ธํธ๊ฐ ํผ ํ๋๋ฅผ ๋ณด๋ด๋ ค๊ณ ํ๋ฉด: |
||||
|
|
||||
|
* `username`: `Rick` |
||||
|
* `password`: `Portal Gun` |
||||
|
* `extra`: `Mr. Poopybutthole` |
||||
|
|
||||
|
`extra` ํ๋๊ฐ ํ์ฉ๋์ง ์๋๋ค๋ ์ค๋ฅ ์๋ต์ ๋ฐ๊ฒ ๋ฉ๋๋ค: |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"detail": [ |
||||
|
{ |
||||
|
"type": "extra_forbidden", |
||||
|
"loc": ["body", "extra"], |
||||
|
"msg": "Extra inputs are not permitted", |
||||
|
"input": "Mr. Poopybutthole" |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## ์์ฝ |
||||
|
|
||||
|
Pydantic ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ FastAPI์์ ํผ ํ๋๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค. ๐ |
Loadingโฆ
Reference in new issue