MCPcopy Index your code
hub / github.com/encode/django-rest-framework / update

Method update

rest_framework/serializers.py:1040–1064  ·  view source on GitHub ↗
(self, instance, validated_data)

Source from the content-addressed store, hash-verified

1038 return instance
1039
1040 def update(self, instance, validated_data):
1041 raise_errors_on_nested_writes('update', self, validated_data)
1042 info = model_meta.get_field_info(instance)
1043
1044 # Simply set each attribute on the instance, and then save it.
1045 # Note that unlike `.create()` we don't need to treat many-to-many
1046 # relationships as being a special case. During updates we already
1047 # have an instance pk for the relationships to be associated with.
1048 m2m_fields = []
1049 for attr, value in validated_data.items():
1050 if attr in info.relations and info.relations[attr].to_many:
1051 m2m_fields.append((attr, value))
1052 else:
1053 setattr(instance, attr, value)
1054
1055 instance.save()
1056
1057 # Note that many-to-many fields are set after updating instance.
1058 # Setting m2m fields triggers signals which could potentially change
1059 # updated instance and we do not want it to collide with .update()
1060 for attr, value in m2m_fields:
1061 field = getattr(instance, attr)
1062 field.set(value)
1063
1064 return instance
1065
1066 # Determine the fields to apply...
1067

Callers

nothing calls this directly

Calls 4

get_field_infoMethod · 0.80
setMethod · 0.80
saveMethod · 0.45

Tested by

no test coverage detected