committed by
GitHub
1 changed files with 56 additions and 0 deletions
@ -0,0 +1,56 @@ |
|||||
|
# ํค๋ ๋งค๊ฐ๋ณ์ ๋ชจ๋ธ |
||||
|
|
||||
|
๊ด๋ จ ์๋ **ํค๋ ๋งค๊ฐ๋ณ์** ๊ทธ๋ฃน์ด ์๋ ๊ฒฝ์ฐ, **Pydantic ๋ชจ๋ธ**์ ์์ฑํ์ฌ ์ ์ธํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
์ด๋ฅผ ํตํด **์ฌ๋ฌ ์์น**์์ **๋ชจ๋ธ์ ์ฌ์ฌ์ฉ** ํ ์ ์๊ณ ๋ชจ๋ ๋งค๊ฐ๋ณ์์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ๋ฐ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ํ ๋ฒ์ ์ ์ธํ ์๋ ์์ต๋๋ค. ๐ |
||||
|
|
||||
|
/// note | ์ฐธ๊ณ |
||||
|
|
||||
|
์ด ๊ธฐ๋ฅ์ FastAPI ๋ฒ์ `0.115.0` ์ดํ๋ถํฐ ์ง์๋ฉ๋๋ค. ๐ค |
||||
|
|
||||
|
/// |
||||
|
|
||||
|
## Pydantic ๋ชจ๋ธ์ ์ฌ์ฉํ ํค๋ ๋งค๊ฐ๋ณ์ |
||||
|
|
||||
|
**Pydantic ๋ชจ๋ธ**์ ํ์ํ **ํค๋ ๋งค๊ฐ๋ณ์**๋ฅผ ์ ์ธํ ๋ค์, ํด๋น ๋งค๊ฐ๋ณ์๋ฅผ `Header`๋ก ์ ์ธํฉ๋๋ค: |
||||
|
|
||||
|
{* ../../docs_src/header_param_models/tutorial001_an_py310.py hl[9:14,18] *} |
||||
|
|
||||
|
**FastAPI**๋ ์์ฒญ์์ ๋ฐ์ **ํค๋**์์ **๊ฐ ํ๋**์ ๋ํ ๋ฐ์ดํฐ๋ฅผ **์ถ์ถ**ํ๊ณ ์ ์ํ Pydantic ๋ชจ๋ธ์ ์ค๋๋ค. |
||||
|
|
||||
|
## ๋ฌธ์ ํ์ธํ๊ธฐ |
||||
|
|
||||
|
๋ฌธ์ UI `/docs`์์ ํ์ํ ํค๋๋ฅผ ๋ณผ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
<div class="screenshot"> |
||||
|
<img src="/img/tutorial/header-param-models/image01.png"> |
||||
|
</div> |
||||
|
|
||||
|
## ์ถ๊ฐ ํค๋ ๊ธ์งํ๊ธฐ |
||||
|
|
||||
|
์ผ๋ถ ํน๋ณํ ์ฌ์ฉ ์ฌ๋ก(ํํ์ง๋ ์๊ฒ ์ง๋ง)์์๋ ์์ ํ๋ ค๋ ํค๋๋ฅผ **์ ํ**ํ ์ ์์ต๋๋ค. |
||||
|
|
||||
|
Pydantic์ ๋ชจ๋ธ ๊ตฌ์ฑ์ ์ฌ์ฉํ์ฌ ์ถ๊ฐ(`extra`) ํ๋๋ฅผ ๊ธ์ง(`forbid`)ํ ์ ์์ต๋๋ค: |
||||
|
|
||||
|
{* ../../docs_src/header_param_models/tutorial002_an_py310.py hl[10] *} |
||||
|
|
||||
|
ํด๋ผ์ด์ธํธ๊ฐ **์ถ๊ฐ ํค๋**๋ฅผ ๋ณด๋ด๋ ค๊ณ ์๋ํ๋ฉด, **์ค๋ฅ** ์๋ต์ ๋ฐ๊ฒ ๋ฉ๋๋ค. |
||||
|
|
||||
|
์๋ฅผ ๋ค์ด, ํด๋ผ์ด์ธํธ๊ฐ `plumbus` ๊ฐ์ผ๋ก `tool` ํค๋๋ฅผ ๋ณด๋ด๋ ค๊ณ ํ๋ฉด, ํด๋ผ์ด์ธํธ๋ ํค๋ ๋งค๊ฐ๋ณ์ `tool`์ด ํ์ฉ ๋์ง ์๋๋ค๋ **์ค๋ฅ** ์๋ต์ ๋ฐ๊ฒ ๋ฉ๋๋ค: |
||||
|
|
||||
|
```json |
||||
|
{ |
||||
|
"detail": [ |
||||
|
{ |
||||
|
"type": "extra_forbidden", |
||||
|
"loc": ["header", "tool"], |
||||
|
"msg": "Extra inputs are not permitted", |
||||
|
"input": "plumbus", |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
``` |
||||
|
|
||||
|
## ์์ฝ |
||||
|
|
||||
|
**Pydantic ๋ชจ๋ธ**์ ์ฌ์ฉํ์ฌ **FastAPI**์์ **ํค๋**๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค. ๐ |
Loadingโฆ
Reference in new issue