![]() |
VOOZH | about |
The response_model acts as a security gate and a data formatter between the internal logic (database, raw data) and the outside logic. Its main features are:
Without a response model, an API may expose sensitive data such as hashed passwords, internal IDs, or timestamps. Defining a response model restricts the output to specified fields, ensuring only intended data is returned to the client.
1. Define Your Schemas: It is a better practice to separate your Input model (what the user sends) from your Output model (what the user sees).
2. Apply the response_model: You apply the model in the path operation decorator, not in the function signature.
Input:
Output:
We can hide fields that haven't been changed or are empty (if required) by using the following decorator parameters:
| Parameter | Effect |
|---|---|
| response_model_exclude_unset | Only includes fields that were actually set in the code. |
| response_model_exclude_none | Removes any field with a null value from the final JSON. |