Request
You can use ormar Models in fastapi request Body parameters instead of pydantic models.
You can of course also mix ormar.Models with pydantic ones if you need to.
One of the most common tasks in requests is excluding certain fields that you do not want to include in the payload you send to API.
This can be achieved in several ways in ormar so below you can review your options and select the one most suitable for your situation.
Excluding fields in request
Optional fields
Note that each field that is optional is not required, that means that Optional fields can be skipped both in response and in requests.
Field is not required if (any/many/all) of following:
- Field is marked with
nullable=True - Field has
defaultvalue or function provided, i.e.default="Test" - Field has a
server_defaultvalue set - Field is an
autoincrement=Trueprimary_keyfield (note thatormar.Integerprimary_keyisautoincrementby default)
Example:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
In above example fields id (is an autoincrement Integer), first_name ( has nullable=True) and category (has default) are optional and can be skipped in response and model will still validate.
If the field is nullable you don't have to include it in payload during creation as well as in response, so given example above you can:
Warning
Note that although you do not have to pass the optional field, you still can do it. And if someone will pass a value it will be used later unless you take measures to prevent it.
| Python | |
|---|---|
1 2 3 4 | |
That means that if you do not pass i.e. first_name in request it will validate correctly (as field is optional), None will be saved in the database.
Generate pydantic model from ormar.Model
Since task of excluding fields is so common ormar has a special way to generate pydantic models from existing ormar.Models without you needing to retype all the fields.
That method is get_pydantic() method available on all models classes.
| Python | |
|---|---|
1 2 3 4 5 6 | |
Note
To see more examples and read more visit get_pydantic part of the documentation.
If you prefer to send and receive related models as their primary keys instead of nested objects, pass them in the flatten_fields parameter (or use flatten_all=True).
| Python | |
|---|---|
1 2 | |
To read more visit relations as primary keys.
Warning
The get_pydantic method generates one named nested model per related model, and relations point to it, so cyclic relations become recursive models ($ref in the OpenAPI schema). Relation fields of nested models are optional and include the relation back to the parent (pass omit_back_relations=True to leave it out, or max_depth=N to limit the nesting).
Names are stable (Item, Item_nested, a hash suffix for include/exclude), so the OpenAPI document does not change between runs. A model nested with different include/exclude gets its own model, so you can still select different fields on different branches.
Mypy and type checking
Note that assigning a function as a python type passes at runtime (as it's not checked) the static type checkers like mypy will complain.
Although result of the function call will always be the same for given model using a dynamically created type is not allowed.
Therefore you have two options:
First one is to simply add # type: ignore to skip the type checking
| Python | |
|---|---|
1 2 3 4 5 | |
The second one is a little bit more hacky and utilizes a way in which fastapi extract function parameters.
You can overwrite the __annotations__ entry for given param.
| Python | |
|---|---|
1 2 3 4 5 6 7 8 | |
Note that this will cause mypy to "think" that user is an ormar model but since in request it doesn't matter that much (you pass jsonized dict anyway and you need to convert before saving).
That still should work fine as generated model will be a subset of fields, so all needed fields will validate, and all not used fields will fail at runtime.
Separate pydantic model
The final solution is to just create separate pydantic model manually. That works exactly the same as with normal fastapi application, so you can have different models for response and requests etc.
Sample:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |