2020-01-18 16:21:54 +01:00
The same way you can declare additional validation and metadata in * path operation function * parameters with `Query` , `Path` and `Body` , you can declare validation and metadata inside of Pydantic models using Pydantic's `Field` .
2018-12-14 19:42:46 +04:00
2019-11-27 11:32:02 -08:00
## Import `Field`
2018-12-14 19:42:46 +04:00
First, you have to import it:
2018-12-18 22:55:30 +04:00
```Python hl_lines="2"
2019-12-09 16:48:54 +03:00
{!./src/body_fields/tutorial001.py!}
2018-12-14 19:42:46 +04:00
` ``
!!! warning
2019-11-27 11:32:02 -08:00
Notice that ` Field` is imported directly from ` pydantic`, not from ` fastapi` as are all the rest (` Query`, ` Path`, ` Body`, etc).
2018-12-14 19:42:46 +04:00
## Declare model attributes
2019-11-27 11:32:02 -08:00
You can then use ` Field` with model attributes:
2018-12-14 19:42:46 +04:00
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="9 10"
2019-12-09 16:48:54 +03:00
{!./src/body_fields/tutorial001.py!}
2018-12-14 19:42:46 +04:00
` ``
2019-11-27 11:32:02 -08:00
` Field` works the same way as ` Query`, ` Path` and ` Body`, it has all the same parameters, etc.
2018-12-14 19:42:46 +04:00
2019-05-15 22:01:23 +04:00
!!! note "Technical Details"
2019-11-27 11:32:02 -08:00
Actually, ` Query`, ` Path` and others you'll see next create objects of subclasses of a common ` Param` class, which is itself a subclass of Pydantic's ` FieldInfo` class.
2018-12-14 19:42:46 +04:00
2019-11-27 11:32:02 -08:00
And Pydantic's ` Field` returns an instance of ` FieldInfo` as well.
2018-12-14 19:42:46 +04:00
2019-11-27 11:32:02 -08:00
` Body` also returns objects of a subclass of ` FieldInfo` directly. And there are others you will see later that are subclasses of the ` Body` class.
2020-01-18 16:21:54 +01:00
Remember that when you import ` Query`, ` Path`, and others from ` fastapi`, those are actually functions that return special classes.
2019-05-15 22:01:23 +04:00
2018-12-14 19:42:46 +04:00
!!! tip
2020-01-18 16:21:54 +01:00
Notice how each model's attribute with a type, default value and ` Field` has the same structure as a *path operation function's* parameter, with ` Field` instead of ` Path`, ` Query` and ` Body`.
2018-12-14 19:42:46 +04:00
2019-12-09 16:48:54 +03:00
## JSON Schema extras
2018-12-14 19:42:46 +04:00
2019-11-27 11:32:02 -08:00
In ` Field`, ` Path`, ` Query`, ` Body` and others you'll see later, you can declare extra parameters apart from those described before.
2018-12-14 19:42:46 +04:00
Those parameters will be added as-is to the output JSON Schema.
2018-12-29 18:43:58 +04:00
If you know JSON Schema and want to add extra information apart from what we have discussed here, you can pass that as extra keyword arguments.
2018-12-14 19:42:46 +04:00
!!! warning
Have in mind that extra parameters passed won't add any validation, only annotation, for documentation purposes.
2020-01-17 16:24:05 +01:00
For example, you can use that functionality to pass a <a href="http://json-schema.org/latest/json-schema-validation.html#rfc.section.8.5" class="external-link" target="_blank">JSON Schema example</a> field to a body request JSON Schema:
2018-12-14 19:42:46 +04:00
2018-12-18 22:55:30 +04:00
` ``Python hl_lines="20 21 22 23 24 25"
2019-12-09 16:48:54 +03:00
{!./src/body_fields/tutorial002.py!}
2018-12-14 19:42:46 +04:00
` ``
2019-01-10 20:52:06 +04:00
And it would look in the ` /docs` like this:
2019-12-09 16:48:54 +03:00
<img src="/img/tutorial/body-fields/image01.png">
2019-01-10 20:52:06 +04:00
2018-12-14 19:42:46 +04:00
## Recap
2019-11-27 11:32:02 -08:00
You can use Pydantic's ` Field` to declare extra validations and metadata for model attributes.
2018-12-14 19:42:46 +04:00
2019-11-27 11:32:02 -08:00
You can also use the extra keyword arguments to pass additional JSON Schema metadata.