@@ -601,6 +601,26 @@ class ModelSerializer(Serializer):
601
601
_related_class = PrimaryKeyRelatedField
602
602
603
603
def create (self , validated_attrs ):
604
+ """
605
+ We have a bit of extra checking around this in order to provide
606
+ descriptive messages when something goes wrong, but this method is
607
+ essentially just:
608
+
609
+ return ExampleModel.objects.create(**validated_attrs)
610
+
611
+ If there are many to many fields present on the instance then they
612
+ cannot be set until the model is instantiated, in which case the
613
+ implementation is like so:
614
+
615
+ example_relationship = validated_attrs.pop('example_relationship')
616
+ instance = ExampleModel.objects.create(**validated_attrs)
617
+ instance.example_relationship = example_relationship
618
+ return instance
619
+
620
+ The default implementation also does not handle nested relationships.
621
+ If you want to support writable nested relationships you'll need
622
+ to write an explicit `.create()` method.
623
+ """
604
624
# Check that the user isn't trying to handle a writable nested field.
605
625
# If we don't do this explicitly they'd likely get a confusing
606
626
# error at the point of calling `Model.objects.create()`.
@@ -651,14 +671,19 @@ def update(self, instance, validated_attrs):
651
671
return instance
652
672
653
673
def get_validators (self ):
674
+ # If the validators have been declared explicitly then use that.
675
+ validators = getattr (getattr (self , 'Meta' , None ), 'validators' , None )
676
+ if validators is not None :
677
+ return validators
678
+
679
+ # Determine the default set of validators.
680
+ validators = []
681
+ model_class = self .Meta .model
654
682
field_names = set ([
655
683
field .source for field in self .fields .values ()
656
684
if (field .source != '*' ) and ('.' not in field .source )
657
685
])
658
686
659
- validators = getattr (getattr (self , 'Meta' , None ), 'validators' , [])
660
- model_class = self .Meta .model
661
-
662
687
# Note that we make sure to check `unique_together` both on the
663
688
# base model class, but also on any parent classes.
664
689
for parent_class in [model_class ] + list (model_class ._meta .parents .keys ()):
0 commit comments