Add readonly 'id' field

This commit is contained in:
Tom Christie 2012-09-25 13:40:16 +01:00
parent 758bcf5b1e
commit 6fc5581a8f

View File

@ -85,6 +85,7 @@ We're going to create a simple Web API that we can use to edit these comment obj
class CommentSerializer(serializers.Serializer): class CommentSerializer(serializers.Serializer):
id = serializers.IntegerField(readonly=True)
email = serializers.EmailField() email = serializers.EmailField()
content = serializers.CharField(max_length=200) content = serializers.CharField(max_length=200)
created = serializers.DateTimeField() created = serializers.DateTimeField()
@ -128,13 +129,13 @@ We've now got a few comment instances to play with. Let's take a look at serial
serializer = CommentSerializer(instance=c1) serializer = CommentSerializer(instance=c1)
serializer.data serializer.data
# {'email': u'leila@example.com', 'content': u'nothing to say', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774, tzinfo=<UTC>)} # {'id': 1, 'email': u'leila@example.com', 'content': u'nothing to say', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774, tzinfo=<UTC>)}
At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into `json`. At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into `json`.
stream = JSONRenderer().render(serializer.data) stream = JSONRenderer().render(serializer.data)
stream stream
# '{"email": "leila@example.com", "content": "nothing to say", "created": "2012-08-22T16:20:09.822"}' # '{"id": 1, "email": "leila@example.com", "content": "nothing to say", "created": "2012-08-22T16:20:09.822"}'
Deserialization is similar. First we parse a stream into python native datatypes... Deserialization is similar. First we parse a stream into python native datatypes...