Skip to content

Commit 78a741b

Browse files
committed
Split out .create and .update on ListSerializer
1 parent fd97d9b commit 78a741b

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

rest_framework/serializers.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -449,26 +449,39 @@ def to_representation(self, data):
449449
serializer=self
450450
)
451451

452+
def update(self, instance, validated_data):
453+
raise NotImplementedError(
454+
"Serializers with many=True do not support multiple update by "
455+
"default, only multiple create. For updates it is unclear how to "
456+
"deal with insertions and deletions. If you need to support "
457+
"multiple update, use a `ListSerializer` class and override "
458+
"`.update()` so you can specify the behavior exactly."
459+
)
460+
461+
def create(self, validated_data):
462+
return [
463+
self.child.create(attrs) for attrs in validated_data
464+
]
465+
452466
def save(self, **kwargs):
453467
"""
454468
Save and return a list of object instances.
455469
"""
456-
assert self.instance is None, (
457-
"Serializers do not support multiple update by default, only "
458-
"multiple create. For updates it is unclear how to deal with "
459-
"insertions and deletions. If you need to support multiple update, "
460-
"use a `ListSerializer` class and override `.save()` so you can "
461-
"specify the behavior exactly."
462-
)
463-
464470
validated_data = [
465471
dict(list(attrs.items()) + list(kwargs.items()))
466472
for attrs in self.validated_data
467473
]
468474

469-
self.instance = [
470-
self.child.create(attrs) for attrs in validated_data
471-
]
475+
if self.instance is not None:
476+
self.instance = self.update(self.instance, validated_data)
477+
assert self.instance is not None, (
478+
'`update()` did not return an object instance.'
479+
)
480+
else:
481+
self.instance = self.create(validated_data)
482+
assert self.instance is not None, (
483+
'`create()` did not return an object instance.'
484+
)
472485

473486
return self.instance
474487

0 commit comments

Comments
 (0)