reply
You can use the ormar model instead of the pydantic model in fastapi response_model.
You can of course mix ormar.Models with pydantic if you want.
One of the most common tasks in a response is to exclude certain fields that you do not want to include in the response data.
There are many ways to achieve this in ormar, so you can review your options below and choose the one that best suits your situation.
Excluding fields in response
optional field
Note that each optional field is not required, which means optional fields can be skipped in both responses and requests.
Fields are not required if (any/many/all) of the following occur:
- Field marked as nullable=True
- field has a default value or provided functionality, i.e. default="Test"
- field has the server_default value set
- Field is an autoincrement=Trueprimary_key field (note that ormar. Integrprimary_key defaults to autoincrement)
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
In the example above, the fields id (which is an autoincrement integer), first_name (with nullable=True), and category (with default values) are optional and can be skipped in the response, and the model will still validate.
If this field is nullable, it does not have to be included in the payload during creation and in the response, so based on the example above, you can:
1 2 3 4 | |
This means that if you do not pass ie first_name in the request, it will validate correctly (because the field is optional), save in the database and return saved records without this field (which will also validate).
!!!Note Note that although you didn't pass the field value, the field itself still exists in the response_model, meaning it will appear in the response data and be set to None.
1 | |
FastApi Response_Model_Exclusion
Fastapi has a response_model_exclude that accepts a group (or list) of field names.
This has its limitations because ormar and pydantic also accept dictionaries where you can set exclusion/inclusion columns as well as on nested models (see more below).
Warning Note that response_model cannot be used to exclude required fields because it fails during validation.
1 2 3 | |
The endpoint above can be queried as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Notice how the password field completely disappears from the response data in the example above.
1 | |
nested model exclusion
Although fastapi allows only one set of field names to be passed, so simply exclude, ormar is smarter when using response_model_exclude.
In ormar, you can use two types of symbols to exclude nested models.
The first is a dictionary with nested fields representing the model tree structure, and the second is a path of field names separated by double underscores.
Suppose our user category is a separate model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
If you want to exclude priority from categories in the response, you can still use the fastapi parameter.
1 2 3 | |
Note that you can double underline deeper models, and if you want to exclude multiple fields from nested models, you need to precede them with full paths. In the example response_model_exclude={"category__priority", "category__other_field", category__nested_model__nested_model_field} ETC.
!!!Note To read more about possible exclusions and how to build an exclusion dictionary or setting, visit the Fields section of the documentation.
Note that in addition to the response_model_exclude parameter, fastapi also supports other parameters inherited from pydantic. All of these also apply to ormar, but there may be some nuances, so it's best to read the dict section of the document.
Excluding in Model.model_dump()
Alternatively, you can just return a dictionary from ormar.Model and use it.
Like this, you can also set exclude/include to dictionary and exclude fields on nested models.
!!!Warning Not using response_model will result in API documents with no response examples and patterns, because responses can theoretically have any format.
1 2 3 4 5 | |
!!!Note that even if you pass the password field in the request, the example above invalidates the password field, but the field will still exist because it is part of the response schema and the value will be set to None.
If you want to completely exclude the field in this way, simply do not use response_model and exclude it in the model_dump() of the model
Alternatively, you can just return a dictionary from the ormar model. Like this, you can also set exclude/include to dictionary and exclude fields on nested models.
!!!Note that theoretically, you lose response validation here, but since you are operating on ormar.Models, the response data is already validated after database queries (because ormar models are pydantic models).
So if you skip response_model altogether, you can do the following:
1 2 3 4 | |
!!!Note that when you skip response_model, you can now also exclude required fields because the response is no longer validated after return.
1 | |
Generating pydantic models from ormar.Model
Since the task of excluding fields is very common, ormar has a special way to generate pydantic models from existing ormar.Models without having to re-enter all the fields.
This method is the get_pydantic() method available to all model classes.
1 2 3 4 5 | |
!!! Note To see more examples and read more, visit the get_pydantic section of the documentation.
Warning The get_pydantic method generates all models in the nested model tree based on algorithms that allow loops in the model to be avoided (the same algorithms used in model_dump(), select_all(), etc.)
1 2 3 | |
Independent cantilever model
The final solution is to manually create a separate pydantic model. It works exactly the same as a regular fastapi application, so you can use different response and request models, etc.
Sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |