Skip to content

Commit 7815811

Browse files
committed
Update nested serialization docs
1 parent 05d8a90 commit 7815811

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

docs/api-guide/relations.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ Nested relationships can be expressed by using serializers as fields.
213213

214214
If the field is used to represent a to-many relationship, you should add the `many=True` flag to the serializer field.
215215

216-
Note that nested relationships are currently read-only. For read-write relationships, you should use a flat relational style.
217-
218216
## Example
219217

220218
For example, the following serializer:

docs/api-guide/serializers.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,21 @@ If a nested representation may optionally accept the `None` value you should pas
177177
content = serializers.CharField(max_length=200)
178178
created = serializers.DateTimeField()
179179

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.
181181

182182
class CommentSerializer(serializers.Serializer):
183183
user = UserSerializer(required=False)
184184
edits = EditItemSerializer(many=True) # A nested list of 'edit' items.
185185
content = serializers.CharField(max_length=200)
186186
created = serializers.DateTimeField()
187187

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.
191189

192-
---
190+
serializer = CommentSerializer(comment, data={'user': {'email': 'foobar', 'user': 'doe'}, 'content': 'baz'})
191+
serializer.is_valid()
192+
# False
193+
serializer.errors
194+
# {'user': {'email': [u'Enter a valid e-mail address.']}, 'created': [u'This field is required.']}
193195

194196
## Dealing with multiple objects
195197

@@ -293,8 +295,7 @@ You can provide arbitrary additional context by passing a `context` argument whe
293295

294296
The context dictionary can be used within any serializer field logic, such as a custom `.to_native()` method, by accessing the `self.context` attribute.
295297

296-
---
297-
298+
-
298299
# ModelSerializer
299300

300301
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
331332

332333
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.
333334

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+
334337
## Specifying which fields should be read-only
335338

336339
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

Comments
 (0)