You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`description: str = None` has a default of `None`.
94
+
*`tax: float = None` has a default of `None`.
95
+
*`tags: List[str] = []` has a default of an empty list: `[]`.
96
+
97
+
You can set the *path operation decorator* parameter `response_model_skip_defaults=True`:
98
+
99
+
```Python hl_lines="24"
100
+
{!./src/response_model/tutorial004.py!}
101
+
```
102
+
103
+
and those default values won't be included in the response.
104
+
105
+
So, if you send a request to that *path operation* for the item with ID `foo`, the response (not including default values) will be:
106
+
107
+
```JSON
108
+
{
109
+
"name": "Foo",
110
+
"price": 50.2
111
+
}
112
+
```
113
+
114
+
!!! info
115
+
FastAPI uses Pydantic model's `.dict()` with <ahref="https://pydantic-docs.helpmanual.io/#copying"target="_blank">its `skip_defaults` parameter</a> to achieve this.
116
+
117
+
### Data with values for fields with defaults
118
+
119
+
But if your data has values for the model's fields with default values, like the item with ID `bar`:
120
+
121
+
```Python hl_lines="3 5"
122
+
{
123
+
"name": "Bar",
124
+
"description": "The bartenders",
125
+
"price": 62,
126
+
"tax": 20.2
127
+
}
128
+
```
129
+
130
+
they will be included in the response.
131
+
132
+
### Data with the same values as the defaults
133
+
134
+
If the data has the same values as the default ones, like the item with ID `baz`:
135
+
136
+
```Python hl_lines="3 5 6"
137
+
{
138
+
"name": "Baz",
139
+
"description": None,
140
+
"price": 50.2,
141
+
"tax": 10.5,
142
+
"tags": []
143
+
}
144
+
```
145
+
146
+
FastAPI is smart enough (actually, Pydantic is smart enough) to realize that, even though `description`, `tax`, and `tags` have the same values as the defaults, they were set explicitly (instead of taken from the defaults).
147
+
148
+
So, they will be included in the JSON response.
149
+
150
+
!!! tip
151
+
Notice that the default values can be anything, not only `None`.
152
+
153
+
They can be a list (`[]`), a `float` of `10.5`, etc.
154
+
155
+
### Use cases
156
+
157
+
This is very useful in several scenarios.
158
+
159
+
For example if you have models with many optional attributes in a NoSQL database, but you don't want to send very long JSON responses full of default values.
160
+
161
+
### Using Pydantic's `skip_defaults` directly
162
+
163
+
You can also use your model's `.dict(skip_defaults=True)` in your code.
164
+
165
+
For example, you could receive a model object as a body payload, and update your stored data using only the attributes set, not the default ones:
166
+
167
+
```Python hl_lines="31 32 33 34 35"
168
+
{!./src/response_model/tutorial004.py!}
169
+
```
170
+
171
+
!!! tip
172
+
It's common to use the HTTP `PUT` operation to update data.
173
+
174
+
In theory, `PUT` should be used to "replace" the entire contents.
175
+
176
+
The less known HTTP `PATCH` operation is also used to update data.
177
+
178
+
But `PATCH` is expected to be used when *partially* updating data. Instead of *replacing* the entire content.
179
+
180
+
Still, this is just a small detail, and many teams and code bases use `PUT` instead of `PATCH` for all updates, including to *partially* update contents.
181
+
182
+
You can use `PUT` or `PATCH` however you wish.
183
+
85
184
## Recap
86
185
87
186
Use the path operation decorator's parameter `response_model` to define response models and especially to ensure private data is filtered out.
187
+
188
+
Use `response_model_skip_defaults` to return only the values explicitly set.
0 commit comments