From a06043a0ce9b65d1dd8ba23a362a4c9fbdf5d6a5 Mon Sep 17 00:00:00 2001 From: Gil Forcada Codinachs Date: Thu, 31 Oct 2019 12:02:58 +0100 Subject: [PATCH] Notify about the fields that changed This allows signal receivers to know specifically what has been changed on the model. --- rest_framework/serializers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index f5d9a5065..1765a90bf 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -979,13 +979,16 @@ class ModelSerializer(Serializer): # relationships as being a special case. During updates we already # have an instance pk for the relationships to be associated with. m2m_fields = [] + changed_fields = [] for attr, value in validated_data.items(): if attr in info.relations and info.relations[attr].to_many: m2m_fields.append((attr, value)) else: - setattr(instance, attr, value) + if getattr(instance, attr, None) != value: + setattr(instance, attr, value) + changed_fields.append(attr) - instance.save() + instance.save(update_fields=changed_fields) # Note that many-to-many fields are set after updating instance. # Setting m2m fields triggers signals which could potentially change