You can use ormar Models in
Body parameters instead of pydantic models.
You can of course also mix
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
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
- Field has
defaultvalue or function provided, i.e.
- Field has a
- Field is an
primary_keyfield (note that
1 2 3 4 5 6 7 8 9 10 11 12
In above example fields
id (is an
first_name ( has
default) are optional and can be skipped in response and model wil 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:
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.
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.
pydantic model from
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.
1 2 3 4 5 6
To see more examples and read more visit get_pydantic part of the documentation.
get_pydantic method generates all models in a tree of nested models according to an algorithm that allows to avoid loops in models (same algorithm that is used in
That means that nested models won't have reference to parent model (by default ormar relation is biderectional).
Note also that if given model exists in a tree more than once it will be doubled in pydantic models (each occurance will have separate own model). That way you can exclude/include different fields on different leafs of the tree.
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
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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17