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
In this case only the first level of `children` will be included
188
188
See [Max recursion](#Max-recursion)
189
189
190
+
Alternatively you can pass the `max_depth` kwarg, which allows to control recursion in general without having to explicitly list each attribute you don't want recursed.
191
+
Please note that this check is performed at the beginning of `serialize_model()`, which means every other nested Iterable is*not affected* by this setting.
192
+
The default value returned when recursion is no longer permitted is a string in the form `tablename.id` (e.g. `users.123`), with fallback to `repr(item)`in case the `id` attribute isnot available for the model.
193
+
This option can be disabled using `max_depth=-1`, which is also the default.
194
+
```python
195
+
# Stop recursion at top level, meaning all relationships will be "dropped"
# Stop at first level, meaning only the first children will be processed
206
+
# For example, the same user above will be serialized to:
207
+
# {
208
+
# "id": 123, "name": "john",
209
+
# "articles": [
210
+
# {"id": 10, "title": ...},
211
+
# {"id": 11, "title": ...},
212
+
# ]
213
+
# }
214
+
item.to_dict(max_depth=1)
215
+
```
216
+
The `max_depth` option can be also configured at mixin level, setting the `serialize_max_depth`class attribute which avoids passing it to each `to_dict` call.
217
+
```python
218
+
# Set max_depth option in mixin
219
+
class SomeModel(db.Model, SerializerMixin):
220
+
serialize_max_depth=0
221
+
222
+
# Call to_dict without max_depth arg
223
+
item.to_dict()
224
+
```
225
+
190
226
# Custom formats
191
227
If you want to change datetime/date/time/decimal formatin one model you can specify it like below:
0 commit comments