From e628d9eb9b7deac2ecffe23eace5c72709887f8f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 6 Mar 2015 12:05:16 +0000 Subject: Update documentation --- api-guide/serializers/index.html | 81 ++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'api-guide/serializers/index.html') diff --git a/api-guide/serializers/index.html b/api-guide/serializers/index.html index 4bb06d3b..b5b73213 100644 --- a/api-guide/serializers/index.html +++ b/api-guide/serializers/index.html @@ -188,6 +188,10 @@ Pagination +
If you only want a subset of the default fields to be used in a model serializer, you can do so using fields or exclude options, just as you would with a ModelForm.
For example:
class AccountSerializer(serializers.ModelSerializer):
@@ -950,7 +946,7 @@ AccountSerializer():
model = Account
Extra fields can correspond to any property or callable on the model.
-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 shortcut Meta option, read_only_fields.
This option should be a list or tuple of field names, and is declared as follows:
class AccountSerializer(serializers.ModelSerializer):
@@ -968,7 +964,7 @@ AccountSerializer():
Please review the Validators Documentation for details on the UniqueTogetherValidator and CurrentUserDefault classes.
There is also a shortcut allowing you to specify arbitrary additional keyword arguments on fields, using the extra_kwargs option. Similarly to read_only_fields this means you do not need to explicitly declare the field on the serializer.
This option is a dictionary, mapping field names to a dictionary of keyword arguments. For example:
class CreateUserSerializer(serializers.ModelSerializer):
@@ -997,6 +993,43 @@ AccountSerializer():
model = Account
Typically we would recommend not using inheritance on inner Meta classes, but instead declaring all options explicitly.
+The ModelSerializer class also exposes an API that you can override in order to alter how serializer fields are automatically determined when instantiating the serializer.
+Normally if a ModelSerializer does not generate the fields you need by default the you should either add them to the class explicitly, or simply use a regular Serializer class instead. However in some cases you may want to create a new base class that defines how the serializer fields are created for any given model.
.serializer_field_mappingA mapping of Django model classes to REST framework serializer classes. You can override this mapping to alter the default serializer classes that should be used for each model class.
+.serializer_related_fieldThis property should be the serializer field class, that is used for relational fields by default.
+For ModelSerializer this defaults to PrimaryKeyRelatedField.
For HyperlinkedModelSerializer this defaults to serializers.HyperlinkedRelatedField.
serializer_url_fieldThe serializer field class that should be used for any url field on the serializer.
Defaults to serializers.HyperlinkedIdentityField
serializer_choice_fieldThe serializer field class that should be used for any choice fields on the serializer.
+Defaults to serializers.ChoiceField
The following methods are called to determine the class and keyword arguments for each field that should be automatically included on the serializer. Each of these methods should return a two tuple of (field_class, field_kwargs).
.build_standard_field(self, field_name, model_field)Called to generate a serializer field that maps to a standard model field.
+The default implementation returns a serializer class based on the serializer_field_mapping attribute.
.build_relational_field(self, field_name, relation_info)Called to generate a serializer field that maps to a relational model field.
+The default implementation returns a serializer class based on the serializer_relational_field attribute.
The relation_info argument is a named tuple, that contains model_field, related_model, to_many and has_through_model properties.
.build_nested_field(self, field_name, relation_info, nested_depth)Called to generate a serializer field that maps to a relational model field, when the depth option has been set.
The default implementation dynamically creates a nested serializer class based on either ModelSerializer or HyperlinkedModelSerializer.
The nested_depth will be the value of the depth option, minus one.
The relation_info argument is a named tuple, that contains model_field, related_model, to_many and has_through_model properties.
.build_property_field(self, field_name, model_class)Called to generate a serializer field that maps to a property or zero-argument method on the model class.
+The default implementation returns a ReadOnlyField class.
.build_url_field(self, field_name, model_class)Called to generate a serializer field for the serializer's own url field. The default implementation returns a HyperlinkedIdentityField class.
.build_unknown_field(self, field_name, model_class)Called when the field name did not map to any model field or model property. +The default implementation raises an error, although subclasses may customize this behavior.
The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys.