@@ -449,26 +449,39 @@ def to_representation(self, data):
449
449
serializer = self
450
450
)
451
451
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
+
452
466
def save (self , ** kwargs ):
453
467
"""
454
468
Save and return a list of object instances.
455
469
"""
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
-
464
470
validated_data = [
465
471
dict (list (attrs .items ()) + list (kwargs .items ()))
466
472
for attrs in self .validated_data
467
473
]
468
474
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
+ )
472
485
473
486
return self .instance
474
487
0 commit comments