@@ -668,20 +668,27 @@ def run_child_validation(self, data):
668668 return self .child .run_validation (data )
669669
670670 original_instance = self .child .instance
671+ had_initial_data = hasattr (self .child , 'initial_data' )
672+ original_initial_data = getattr (self .child , 'initial_data' , None )
673+
671674 try :
672675 if (
673676 hasattr (self , '_list_serializer_instance_map' ) and
674- isinstance (data , Mapping ) and
675- original_instance is self .instance
677+ isinstance (data , Mapping )
676678 ):
677- data_pk = data .get ('id' )
678- if data_pk is None :
679- data_pk = data .get ('pk' )
680- self .child .instance = self ._list_serializer_instance_map .get (str (data_pk )) if data_pk is not None else None
679+ data_pk = data .get ('id' ) or data .get ('pk' )
680+ self .child .instance = (self ._list_serializer_instance_map .get (str (data_pk ))
681+ if data_pk is not None else None )
681682
683+ self .child .initial_data = data
682684 return self .child .run_validation (data )
683685 finally :
684686 self .child .instance = original_instance
687+ if had_initial_data :
688+ self .child .initial_data = original_initial_data
689+ else :
690+ if hasattr (self .child , 'initial_data' ):
691+ del self .child .initial_data
685692
686693 def to_internal_value (self , data ):
687694 """
@@ -804,7 +811,7 @@ def save(self, **kwargs):
804811 "inspect 'serializer.validated_data' instead. "
805812 "You can also pass additional keyword arguments to 'save()' if you "
806813 "need to set extra attributes on the saved model instance. "
807- "For example: 'serializer.save(owner=request.user)'.' "
814+ "For example: 'serializer.save(owner=request.user)'."
808815 )
809816 assert not hasattr (self , '_data' ), (
810817 "You cannot call `.save()` after accessing `serializer.data`."
0 commit comments