mirror of
https://github.com/fastapi/fastapi.git
synced 2026-10-22 18:06:17 -04:00
✨ Add support for forbidding extra form fields with Pydantic models (#12134)
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
This commit is contained in:
1 parent
1b06b53267
commit
4633b1bca9
14 files changed
+1360
-3
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# Form Models
|
||||
|
||||
You can use Pydantic models to declare form fields in FastAPI.
|
||||
You can use **Pydantic models** to declare **form fields** in FastAPI.
|
||||
|
||||
/// info
|
||||
|
||||
@@ -22,7 +22,7 @@ This is supported since FastAPI version `0.113.0`. 🤓
|
||||
|
||||
## Pydantic Models for Forms
|
||||
|
||||
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`:
|
||||
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`:
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
@@ -54,7 +54,7 @@ Prefer to use the `Annotated` version if possible.
|
||||
|
||||
////
|
||||
|
||||
FastAPI will extract the data for each field from the form data in the request and give you the Pydantic model you defined.
|
||||
**FastAPI** will **extract** the data for **each field** from the **form data** in the request and give you the Pydantic model you defined.
|
||||
|
||||
## Check the Docs
|
||||
|
||||
@@ -63,3 +63,72 @@ You can verify it in the docs UI at `/docs`:
|
||||
<div class="screenshot">
|
||||
<img src="/img/tutorial/request-form-models/image01.png">
|
||||
</div>
|
||||
|
||||
## Restrict Extra Form Fields
|
||||
|
||||
In some special use cases (probably not very common), you might want to **restrict** the form fields to only those declared in the Pydantic model. And **forbid** any **extra** fields.
|
||||
|
||||
/// note
|
||||
|
||||
This is supported since FastAPI version `0.114.0`. 🤓
|
||||
|
||||
///
|
||||
|
||||
You can use Pydantic's model configuration to `forbid` any `extra` fields:
|
||||
|
||||
//// tab | Python 3.9+
|
||||
|
||||
```Python hl_lines="12"
|
||||
{!> ../../../docs_src/request_form_models/tutorial002_an_py39.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+
|
||||
|
||||
```Python hl_lines="11"
|
||||
{!> ../../../docs_src/request_form_models/tutorial002_an.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
//// tab | Python 3.8+ non-Annotated
|
||||
|
||||
/// tip
|
||||
|
||||
Prefer to use the `Annotated` version if possible.
|
||||
|
||||
///
|
||||
|
||||
```Python hl_lines="10"
|
||||
{!> ../../../docs_src/request_form_models/tutorial002.py!}
|
||||
```
|
||||
|
||||
////
|
||||
|
||||
If a client tries to send some extra data, they will receive an **error** response.
|
||||
|
||||
For example, if the client tries to send the form fields:
|
||||
|
||||
* `username`: `Rick`
|
||||
* `password`: `Portal Gun`
|
||||
* `extra`: `Mr. Poopybutthole`
|
||||
|
||||
They will receive an error response telling them that the field `extra` is not allowed:
|
||||
|
||||
```json
|
||||
{
|
||||
"detail": [
|
||||
{
|
||||
"type": "extra_forbidden",
|
||||
"loc": ["body", "extra"],
|
||||
"msg": "Extra inputs are not permitted",
|
||||
"input": "Mr. Poopybutthole"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Summary
|
||||
|
||||
You can use Pydantic models to declare form fields in FastAPI. 😎
|
||||
Reference in new issue
Block a user