From 3c57e08f62ab4491da18c7174638dc2a52577ac0 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 14 Jul 2015 12:22:51 +0100 Subject: [PATCH] Clarifications to read_only fields. Closes #3064. --- docs/api-guide/fields.md | 2 ++ rest_framework/serializers.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 18637f21c..c5c658314 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -20,6 +20,8 @@ Each serializer field class constructor takes at least these arguments. Some Fi ### `read_only` +Read-only fields are included in the API output, but should not be included in the input during create or update operations. Any 'read_only' fields that are incorrectly included in the serializer input will be ignored. + Set this to `True` to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization. Defaults to `False` diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 1792ae2c4..beaae1e12 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -825,6 +825,10 @@ class ModelSerializer(Serializer): def update(self, instance, validated_data): raise_errors_on_nested_writes('update', self, validated_data) + # Simply set each attribute on the instance, and then save it. + # Note that unlike `.create()` we don't need to treat many-to-many + # relationships as being a special case. During updates we already + # have an instance pk for the relationships to be associated with. for attr, value in validated_data.items(): setattr(instance, attr, value) instance.save()