@@ -107,21 +107,21 @@ where some of the attributes of an object might not be simple datatypes such as
...
@@ -107,21 +107,21 @@ where some of the attributes of an object might not be simple datatypes such as
The `Serializer` class is itself a type of `Field`, and can be used to represent relationships where one object type is nested inside another.
The `Serializer` class is itself a type of `Field`, and can be used to represent relationships where one object type is nested inside another.
class UserSerializer(serializers.Serializer):
class UserSerializer(serializers.Serializer):
email = serializers.EmailField()
email = serializers.Field()
username = serializers.CharField()
username = serializers.Field()
def restore_object(self, attrs, instance=None):
return User(**attrs)
class CommentSerializer(serializers.Serializer):
class CommentSerializer(serializers.Serializer):
user = UserSerializer()
user = UserSerializer()
title = serializers.CharField()
title = serializers.Field()
content = serializers.CharField(max_length=200)
content = serializers.Field()
created = serializers.DateTimeField()
created = serializers.Field()
---
**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.
---
def restore_object(self, attrs, instance=None):
return Comment(**attrs)
## Creating custom fields
## Creating custom fields
...
@@ -225,40 +225,54 @@ For example:
...
@@ -225,40 +225,54 @@ For example:
## Specifiying nested serialization
## Specifiying nested serialization
The default `ModelSerializer` uses primary keys for relationships, but you can also easily generate nested representations using the `nested` option:
The default `ModelSerializer` uses primary keys for relationships, but you can also easily generate nested representations using the `depth` option:
class AccountSerializer(serializers.ModelSerializer):
class AccountSerializer(serializers.ModelSerializer):
class Meta:
class Meta:
model = Account
model = Account
exclude = ('id',)
exclude = ('id',)
nested = True
depth = 1
The `nested` option may be set to either `True`, `False`, or an integer value. If given an integer value it indicates the depth of relationships that should be traversed before reverting to a flat representation.
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.
When serializing objects using a nested representation any occurances of recursion will be recognised, and will fall back to using a flat representation.
## Customising the default fields
## Customising the default fields used by a ModelSerializer
You can create customized subclasses of `ModelSerializer` that use a different set of default fields for the representation, by overriding various `get_<field_type>_field` methods.
Each of these methods may either return a field or serializer instance, or `None`.
### get_pk_field
class AccountSerializer(serializers.ModelSerializer):
**Signature**: `.get_pk_field(self, model_field)`
class Meta:
model = Account
def get_pk_field(self, model_field):
Returns the field instance that should be used to represent the pk field.
Returns the field instance that should be used to represent a related field when `depth` is not specified, or when nested representations are being used and the depth reaches zero.