From c45fe115b872dd4fce24e930fcf6386894786dc9 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 14 Jan 2014 11:28:20 +0000 Subject: Docs update for version 2.3.11 --- api-guide/serializers.html | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'api-guide/serializers.html') diff --git a/api-guide/serializers.html b/api-guide/serializers.html index a2794bd6..898b6ea4 100644 --- a/api-guide/serializers.html +++ b/api-guide/serializers.html @@ -181,6 +181,7 @@
When deserializing data, we can either create a new instance, or update an existing instance.
serializer = CommentSerializer(data=data) # Create new instance
-serializer = CommentSerializer(comment, data=data) # Update `instance`
+serializer = CommentSerializer(comment, data=data) # Update `comment`
By default, serializers must be passed values for all required fields or they will throw validation errors. You can use the partial
argument in order to allow partial updates.
serializer = CommentSerializer(comment, data={'content': u'foo bar'}, partial=True) # Update `instance` with partial data
+serializer = CommentSerializer(comment, data={'content': u'foo bar'}, partial=True) # Update `comment` with partial data
Validation
When deserializing data, you always need to call is_valid()
before attempting to access the deserialized object. If any validation errors occur, the .errors
property will contain a dictionary representing the resulting error messages. For example:
@@ -368,7 +369,7 @@ class CommentSerializer(serializers.Serializer):
created = serializers.DateTimeField()
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.
-serializer = CommentSerializer(comment, data={'user': {'email': 'foobar', 'username': 'doe'}, 'content': 'baz'})
+serializer = CommentSerializer(data={'user': {'email': 'foobar', 'username': 'doe'}, 'content': 'baz'})
serializer.is_valid()
# False
serializer.errors
@@ -494,6 +495,23 @@ The ModelSerializer
class lets you automatically create a Serialize
read_only_fields = ('account_name',)
Model fields which have editable=False
set, and AutoField
fields will be set to read-only by default, and do not need to be added to the read_only_fields
option.
+Specifying which fields should be write-only
+You may wish to specify multiple fields as write-only. Instead of adding each field explicitly with the write_only=True
attribute, you may use the write_only_fields
Meta option, like so:
+class CreateUserSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = User
+ fields = ('email', 'username', 'password')
+ write_only_fields = ('password',) # Note: Password field is write-only
+
+def restore_object(self, attrs, instance=None):
+ """
+ Instantiate a new User instance.
+ """
+ assert instance is None, 'Cannot update users with CreateUserSerializer'
+ user = User(email=attrs['email'], username=attrs['username'])
+ user.set_password(attrs['password'])
+ return user
+
Specifying fields explicitly
You can add extra fields to a ModelSerializer
or override the default fields by declaring fields on the class, just as you would for a Serializer
class.
class AccountSerializer(serializers.ModelSerializer):
--
cgit v1.2.3