Commit ef0caf64 by Tom Christie

Extra note on method

parent e80d3d1b
...@@ -25,6 +25,7 @@ Let's start by creating a simple object we can use for example purposes: ...@@ -25,6 +25,7 @@ Let's start by creating a simple object we can use for example purposes:
comment = Comment(email='leila@example.com', content='foo bar') comment = Comment(email='leila@example.com', content='foo bar')
We'll declare a serializer that we can use to serialize and deserialize `Comment` objects. We'll declare a serializer that we can use to serialize and deserialize `Comment` objects.
Declaring a serializer looks very similar to declaring a form: Declaring a serializer looks very similar to declaring a form:
class CommentSerializer(serializers.Serializer): class CommentSerializer(serializers.Serializer):
...@@ -33,6 +34,13 @@ Declaring a serializer looks very similar to declaring a form: ...@@ -33,6 +34,13 @@ Declaring a serializer looks very similar to declaring a form:
created = serializers.DateTimeField() created = serializers.DateTimeField()
def restore_object(self, attrs, instance=None): def restore_object(self, attrs, instance=None):
"""
Given a dictionary of deserialized field values, either update
an existing model instance, or create a new model instance.
Note that if we don't define this method, then deserializing
data will simply return a dictionary of items.
"""
if instance is not None: if instance is not None:
instance.title = attrs['title'] instance.title = attrs['title']
instance.content = attrs['content'] instance.content = attrs['content']
......
...@@ -126,7 +126,11 @@ The first thing we need to get started on our Web API is provide a way of serial ...@@ -126,7 +126,11 @@ The first thing we need to get started on our Web API is provide a way of serial
def restore_object(self, attrs, instance=None): def restore_object(self, attrs, instance=None):
""" """
Create or update a new snippet instance. Create or update a new snippet instance, given a dictionary
of deserialized field values.
Note that if we don't define this method, then deserializing
data will simply return a dictionary of items.
""" """
if instance: if instance:
# Update existing instance # Update existing instance
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment