STRICT_PARTIAL_UPDATE=True enforce partial

using a new configuration flag to decide if to enforce a partial update
This commit is contained in:
Christian 2022-05-14 21:39:46 +01:00 committed by GitHub
parent cdc956a96c
commit f555c79724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -997,12 +997,17 @@ class ModelSerializer(Serializer):
# relationships as being a special case. During updates we already # relationships as being a special case. During updates we already
# have an instance pk for the relationships to be associated with. # have an instance pk for the relationships to be associated with.
m2m_fields = [] m2m_fields = []
update_fields = []
for attr, value in validated_data.items(): for attr, value in validated_data.items():
if attr in info.relations and info.relations[attr].to_many: if attr in info.relations and info.relations[attr].to_many:
m2m_fields.append((attr, value)) m2m_fields.append((attr, value))
else: else:
setattr(instance, attr, value) setattr(instance, attr, value)
update_fields.append(attr)
if self.partial and api_settings.STRICT_PARTIAL_UPDATE:
instance.save(update_fields=update_fields)
else:
instance.save() instance.save()
# Note that many-to-many fields are set after updating instance. # Note that many-to-many fields are set after updating instance.