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
187
188
See [Max recursion](#Max-recursion)
188
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
+
189
226
# Custom formats
190
227
If you want to change datetime/date/time/decimal formatin one model you can specify it like below:
191
228
```python
@@ -304,6 +341,27 @@ Unfortunately you can not access formats or tzinfo in that functions.
304
341
I'll implement this logic later if any of users needs it.
305
342
306
343
344
+
# Custom Serializer
345
+
To have full control over the Serializer, you can define your own subclass with custom logic, and then configure the mixin to use it.
346
+
```python
347
+
from sqlalchemy_serializer import Serializer, SerializerMixin
348
+
349
+
350
+
class CustomSerializer(Serializer):
351
+
352
+
def serialize_model(self, value) ->dict:
353
+
"""Custom override adding special case for a complex model."""
354
+
ifisinstance(value, ComplexModel):
355
+
return complex_logic(value)
356
+
returnsuper().serialize_model(value)
357
+
358
+
359
+
class CustomSerializerMixin(SerializerMixin):
360
+
361
+
serialize_class= CustomSerializer
362
+
```
363
+
364
+
307
365
# Timezones
308
366
To keep `datetimes` consistent its better to store it in the database normalized to **UTC**.
309
367
But when you return response, sometimes (mostly in web, mobile applications can do it themselves)
0 commit comments