mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 02:50:06 +03:00
Use the update_fields argument when saving the instance.
By doing so, this will trigger a query with only the specified field, which is: a. More efficient b. Prevents some race conditions.
This commit is contained in:
parent
030119c117
commit
36d939a151
|
@ -974,13 +974,15 @@ class ModelSerializer(Serializer):
|
||||||
# Note that unlike `.create()` we don't need to treat many-to-many
|
# Note that unlike `.create()` we don't need to treat many-to-many
|
||||||
# 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.
|
||||||
|
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:
|
||||||
field = getattr(instance, attr)
|
field = getattr(instance, attr)
|
||||||
field.set(value)
|
field.set(value)
|
||||||
else:
|
else:
|
||||||
setattr(instance, attr, value)
|
setattr(instance, attr, value)
|
||||||
instance.save()
|
update_fields.append(attr)
|
||||||
|
instance.save(update_fields=update_fields)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user