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
Copy file name to clipboardExpand all lines: docs/api-guide/serializers.md
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -177,19 +177,21 @@ If a nested representation may optionally accept the `None` value you should pas
177
177
content = serializers.CharField(max_length=200)
178
178
created = serializers.DateTimeField()
179
179
180
-
Similarly if a nested representation should be a list of items, you should the `many=True` flag to the nested serialized.
180
+
Similarly if a nested representation should be a list of items, you should pass the `many=True` flag to the nested serialized.
181
181
182
182
class CommentSerializer(serializers.Serializer):
183
183
user = UserSerializer(required=False)
184
184
edits = EditItemSerializer(many=True) # A nested list of 'edit' items.
185
185
content = serializers.CharField(max_length=200)
186
186
created = serializers.DateTimeField()
187
187
188
-
---
189
-
190
-
**Note**: Nested serializers are only suitable for read-only representations, as there are cases where they would have ambiguous or non-obvious behavior if used when updating instances. For read-write representations you should always use a flat representation, by using one of the `RelatedField` subclasses.
188
+
Validation of nested objects will work the same as before. Errors with nested objects will be nested under the field name of the nested object.
# {'user': {'email': [u'Enter a valid e-mail address.']}, 'created': [u'This field is required.']}
193
195
194
196
## Dealing with multiple objects
195
197
@@ -293,8 +295,7 @@ You can provide arbitrary additional context by passing a `context` argument whe
293
295
294
296
The context dictionary can be used within any serializer field logic, such as a custom `.to_native()` method, by accessing the `self.context` attribute.
295
297
296
-
---
297
-
298
+
-
298
299
# ModelSerializer
299
300
300
301
Often you'll want serializer classes that map closely to model definitions.
@@ -331,6 +332,8 @@ The default `ModelSerializer` uses primary keys for relationships, but you can a
331
332
332
333
The `depth` option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation.
333
334
335
+
If you want to customize the way the serialization is done (e.g. using `allow_add_remove`) you'll need to define the field yourself.
336
+
334
337
## Specifying which fields should be read-only
335
338
336
339
You may wish to specify multiple fields as read-only. Instead of adding each field explicitly with the `read_only=True` attribute, you may use the `read_only_fields` Meta option, like so:
0 commit comments