Skip to content

Commit 7bad7c0

Browse files
authored
✨ Add support for Pydantic models in Form parameters (#12129)
Revert "⏪️ Temporarily revert "✨ Add support for Pydantic models in `Form` pa…" This reverts commit 8e6cf9e.
1 parent 965fc83 commit 7bad7c0

File tree

13 files changed

+994
-3
lines changed

13 files changed

+994
-3
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Form Models
2+
3+
You can use Pydantic models to declare form fields in FastAPI.
4+
5+
/// info
6+
7+
To use forms, first install <a href="https://github.com/Kludex/python-multipart" class="external-link" target="_blank">`python-multipart`</a>.
8+
9+
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then install it, for example:
10+
11+
```console
12+
$ pip install python-multipart
13+
```
14+
15+
///
16+
17+
/// note
18+
19+
This is supported since FastAPI version `0.113.0`. 🤓
20+
21+
///
22+
23+
## Pydantic Models for Forms
24+
25+
You just need to declare a Pydantic model with the fields you want to receive as form fields, and then declare the parameter as `Form`:
26+
27+
//// tab | Python 3.9+
28+
29+
```Python hl_lines="9-11 15"
30+
{!> ../../../docs_src/request_form_models/tutorial001_an_py39.py!}
31+
```
32+
33+
////
34+
35+
//// tab | Python 3.8+
36+
37+
```Python hl_lines="8-10 14"
38+
{!> ../../../docs_src/request_form_models/tutorial001_an.py!}
39+
```
40+
41+
////
42+
43+
//// tab | Python 3.8+ non-Annotated
44+
45+
/// tip
46+
47+
Prefer to use the `Annotated` version if possible.
48+
49+
///
50+
51+
```Python hl_lines="7-9 13"
52+
{!> ../../../docs_src/request_form_models/tutorial001.py!}
53+
```
54+
55+
////
56+
57+
FastAPI will extract the data for each field from the form data in the request and give you the Pydantic model you defined.
58+
59+
## Check the Docs
60+
61+
You can verify it in the docs UI at `/docs`:
62+
63+
<div class="screenshot">
64+
<img src="/img/tutorial/request-form-models/image01.png">
65+
</div>

docs/en/mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ nav:
129129
- tutorial/extra-models.md
130130
- tutorial/response-status-code.md
131131
- tutorial/request-forms.md
132+
- tutorial/request-form-models.md
132133
- tutorial/request-files.md
133134
- tutorial/request-forms-and-files.md
134135
- tutorial/handling-errors.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from fastapi import FastAPI, Form
2+
from pydantic import BaseModel
3+
4+
app = FastAPI()
5+
6+
7+
class FormData(BaseModel):
8+
username: str
9+
password: str
10+
11+
12+
@app.post("/login/")
13+
async def login(data: FormData = Form()):
14+
return data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from fastapi import FastAPI, Form
2+
from pydantic import BaseModel
3+
from typing_extensions import Annotated
4+
5+
app = FastAPI()
6+
7+
8+
class FormData(BaseModel):
9+
username: str
10+
password: str
11+
12+
13+
@app.post("/login/")
14+
async def login(data: Annotated[FormData, Form()]):
15+
return data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Annotated
2+
3+
from fastapi import FastAPI, Form
4+
from pydantic import BaseModel
5+
6+
app = FastAPI()
7+
8+
9+
class FormData(BaseModel):
10+
username: str
11+
password: str
12+
13+
14+
@app.post("/login/")
15+
async def login(data: Annotated[FormData, Form()]):
16+
return data

fastapi/dependencies/utils.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
field_annotation_is_scalar,
3434
get_annotation_from_field_info,
3535
get_missing_field_error,
36+
get_model_fields,
3637
is_bytes_field,
3738
is_bytes_sequence_field,
3839
is_scalar_field,
@@ -56,6 +57,7 @@
5657
from fastapi.security.oauth2 import OAuth2, SecurityScopes
5758
from fastapi.security.open_id_connect_url import OpenIdConnect
5859
from fastapi.utils import create_model_field, get_path_param_names
60+
from pydantic import BaseModel
5961
from pydantic.fields import FieldInfo
6062
from starlette.background import BackgroundTasks as StarletteBackgroundTasks
6163
from starlette.concurrency import run_in_threadpool
@@ -743,7 +745,9 @@ def _should_embed_body_fields(fields: List[ModelField]) -> bool:
743745
return True
744746
# If it's a Form (or File) field, it has to be a BaseModel to be top level
745747
# otherwise it has to be embedded, so that the key value pair can be extracted
746-
if isinstance(first_field.field_info, params.Form):
748+
if isinstance(first_field.field_info, params.Form) and not lenient_issubclass(
749+
first_field.type_, BaseModel
750+
):
747751
return True
748752
return False
749753

@@ -783,7 +787,8 @@ async def process_fn(
783787
for sub_value in value:
784788
tg.start_soon(process_fn, sub_value.read)
785789
value = serialize_sequence_value(field=field, value=results)
786-
values[field.name] = value
790+
if value is not None:
791+
values[field.name] = value
787792
return values
788793

789794

@@ -798,8 +803,14 @@ async def request_body_to_args(
798803
single_not_embedded_field = len(body_fields) == 1 and not embed_body_fields
799804
first_field = body_fields[0]
800805
body_to_process = received_body
806+
807+
fields_to_extract: List[ModelField] = body_fields
808+
809+
if single_not_embedded_field and lenient_issubclass(first_field.type_, BaseModel):
810+
fields_to_extract = get_model_fields(first_field.type_)
811+
801812
if isinstance(received_body, FormData):
802-
body_to_process = await _extract_form_body(body_fields, received_body)
813+
body_to_process = await _extract_form_body(fields_to_extract, received_body)
803814

804815
if single_not_embedded_field:
805816
loc: Tuple[str, ...] = ("body",)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import subprocess
2+
import time
3+
4+
import httpx
5+
from playwright.sync_api import Playwright, sync_playwright
6+
7+
8+
# Run playwright codegen to generate the code below, copy paste the sections in run()
9+
def run(playwright: Playwright) -> None:
10+
browser = playwright.chromium.launch(headless=False)
11+
context = browser.new_context()
12+
page = context.new_page()
13+
page.goto("http://localhost:8000/docs")
14+
page.get_by_role("button", name="POST /login/ Login").click()
15+
page.get_by_role("button", name="Try it out").click()
16+
page.screenshot(path="docs/en/docs/img/tutorial/request-form-models/image01.png")
17+
18+
# ---------------------
19+
context.close()
20+
browser.close()
21+
22+
23+
process = subprocess.Popen(
24+
["fastapi", "run", "docs_src/request_form_models/tutorial001.py"]
25+
)
26+
try:
27+
for _ in range(3):
28+
try:
29+
response = httpx.get("http://localhost:8000/docs")
30+
except httpx.ConnectError:
31+
time.sleep(1)
32+
break
33+
with sync_playwright() as playwright:
34+
run(playwright)
35+
finally:
36+
process.terminate()

tests/test_forms_single_model.py

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from typing import List, Optional
2+
3+
from dirty_equals import IsDict
4+
from fastapi import FastAPI, Form
5+
from fastapi.testclient import TestClient
6+
from pydantic import BaseModel
7+
from typing_extensions import Annotated
8+
9+
app = FastAPI()
10+
11+
12+
class FormModel(BaseModel):
13+
username: str
14+
lastname: str
15+
age: Optional[int] = None
16+
tags: List[str] = ["foo", "bar"]
17+
18+
19+
@app.post("/form/")
20+
def post_form(user: Annotated[FormModel, Form()]):
21+
return user
22+
23+
24+
client = TestClient(app)
25+
26+
27+
def test_send_all_data():
28+
response = client.post(
29+
"/form/",
30+
data={
31+
"username": "Rick",
32+
"lastname": "Sanchez",
33+
"age": "70",
34+
"tags": ["plumbus", "citadel"],
35+
},
36+
)
37+
assert response.status_code == 200, response.text
38+
assert response.json() == {
39+
"username": "Rick",
40+
"lastname": "Sanchez",
41+
"age": 70,
42+
"tags": ["plumbus", "citadel"],
43+
}
44+
45+
46+
def test_defaults():
47+
response = client.post("/form/", data={"username": "Rick", "lastname": "Sanchez"})
48+
assert response.status_code == 200, response.text
49+
assert response.json() == {
50+
"username": "Rick",
51+
"lastname": "Sanchez",
52+
"age": None,
53+
"tags": ["foo", "bar"],
54+
}
55+
56+
57+
def test_invalid_data():
58+
response = client.post(
59+
"/form/",
60+
data={
61+
"username": "Rick",
62+
"lastname": "Sanchez",
63+
"age": "seventy",
64+
"tags": ["plumbus", "citadel"],
65+
},
66+
)
67+
assert response.status_code == 422, response.text
68+
assert response.json() == IsDict(
69+
{
70+
"detail": [
71+
{
72+
"type": "int_parsing",
73+
"loc": ["body", "age"],
74+
"msg": "Input should be a valid integer, unable to parse string as an integer",
75+
"input": "seventy",
76+
}
77+
]
78+
}
79+
) | IsDict(
80+
# TODO: remove when deprecating Pydantic v1
81+
{
82+
"detail": [
83+
{
84+
"loc": ["body", "age"],
85+
"msg": "value is not a valid integer",
86+
"type": "type_error.integer",
87+
}
88+
]
89+
}
90+
)
91+
92+
93+
def test_no_data():
94+
response = client.post("/form/")
95+
assert response.status_code == 422, response.text
96+
assert response.json() == IsDict(
97+
{
98+
"detail": [
99+
{
100+
"type": "missing",
101+
"loc": ["body", "username"],
102+
"msg": "Field required",
103+
"input": {"tags": ["foo", "bar"]},
104+
},
105+
{
106+
"type": "missing",
107+
"loc": ["body", "lastname"],
108+
"msg": "Field required",
109+
"input": {"tags": ["foo", "bar"]},
110+
},
111+
]
112+
}
113+
) | IsDict(
114+
# TODO: remove when deprecating Pydantic v1
115+
{
116+
"detail": [
117+
{
118+
"loc": ["body", "username"],
119+
"msg": "field required",
120+
"type": "value_error.missing",
121+
},
122+
{
123+
"loc": ["body", "lastname"],
124+
"msg": "field required",
125+
"type": "value_error.missing",
126+
},
127+
]
128+
}
129+
)

tests/test_tutorial/test_request_form_models/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)