@@ -608,20 +608,20 @@ class ModelSerializer(Serializer):
608
608
})
609
609
_related_class = PrimaryKeyRelatedField
610
610
611
- def create (self , validated_attrs ):
611
+ def create (self , validated_data ):
612
612
"""
613
613
We have a bit of extra checking around this in order to provide
614
614
descriptive messages when something goes wrong, but this method is
615
615
essentially just:
616
616
617
- return ExampleModel.objects.create(**validated_attrs )
617
+ return ExampleModel.objects.create(**validated_data )
618
618
619
619
If there are many to many fields present on the instance then they
620
620
cannot be set until the model is instantiated, in which case the
621
621
implementation is like so:
622
622
623
- example_relationship = validated_attrs .pop('example_relationship')
624
- instance = ExampleModel.objects.create(**validated_attrs )
623
+ example_relationship = validated_data .pop('example_relationship')
624
+ instance = ExampleModel.objects.create(**validated_data )
625
625
instance.example_relationship = example_relationship
626
626
return instance
627
627
@@ -633,8 +633,8 @@ def create(self, validated_attrs):
633
633
# If we don't do this explicitly they'd likely get a confusing
634
634
# error at the point of calling `Model.objects.create()`.
635
635
assert not any (
636
- isinstance (field , BaseSerializer ) and not field . read_only
637
- for field in self .fields .values ()
636
+ isinstance (field , BaseSerializer ) and ( key in validated_attrs )
637
+ for key , field in self .fields .items ()
638
638
), (
639
639
'The `.create()` method does not suport nested writable fields '
640
640
'by default. Write an explicit `.create()` method for serializer '
@@ -644,17 +644,17 @@ def create(self, validated_attrs):
644
644
645
645
ModelClass = self .Meta .model
646
646
647
- # Remove many-to-many relationships from validated_attrs .
647
+ # Remove many-to-many relationships from validated_data .
648
648
# They are not valid arguments to the default `.create()` method,
649
649
# as they require that the instance has already been saved.
650
650
info = model_meta .get_field_info (ModelClass )
651
651
many_to_many = {}
652
652
for field_name , relation_info in info .relations .items ():
653
- if relation_info .to_many and (field_name in validated_attrs ):
654
- many_to_many [field_name ] = validated_attrs .pop (field_name )
653
+ if relation_info .to_many and (field_name in validated_data ):
654
+ many_to_many [field_name ] = validated_data .pop (field_name )
655
655
656
656
try :
657
- instance = ModelClass .objects .create (** validated_attrs )
657
+ instance = ModelClass .objects .create (** validated_data )
658
658
except TypeError as exc :
659
659
msg = (
660
660
'Got a `TypeError` when calling `%s.objects.create()`. '
@@ -679,18 +679,18 @@ def create(self, validated_attrs):
679
679
680
680
return instance
681
681
682
- def update (self , instance , validated_attrs ):
682
+ def update (self , instance , validated_data ):
683
683
assert not any (
684
- isinstance (field , BaseSerializer ) and not field . read_only
685
- for field in self .fields .values ()
684
+ isinstance (field , BaseSerializer ) and ( key in validated_attrs )
685
+ for key , field in self .fields .items ()
686
686
), (
687
687
'The `.update()` method does not suport nested writable fields '
688
688
'by default. Write an explicit `.update()` method for serializer '
689
689
'`%s.%s`, or set `read_only=True` on nested serializer fields.' %
690
690
(self .__class__ .__module__ , self .__class__ .__name__ )
691
691
)
692
692
693
- for attr , value in validated_attrs .items ():
693
+ for attr , value in validated_data .items ():
694
694
setattr (instance , attr , value )
695
695
instance .save ()
696
696
return instance
0 commit comments