Skip to content

Commit 270c7ac

Browse files
committed
Minor validtors tweak
1 parent 22c5b86 commit 270c7ac

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

docs/topics/3.0-announcement.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,11 @@ The `UniqueTogetherValidator` should be applied to a serializer, and takes a `qu
727727
position = serializers.IntegerField()
728728
name = serializers.CharField(max_length=100)
729729

730-
default_validators = [UniqueTogetherValidator(
731-
queryset=RaceResult.objects.all(),
732-
fields=('category', 'position')
733-
)]
730+
class Meta:
731+
validators = [UniqueTogetherValidator(
732+
queryset=RaceResult.objects.all(),
733+
fields=('category', 'position')
734+
)]
734735

735736
#### The `UniqueForDateValidator` classes.
736737

rest_framework/serializers.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,26 @@ class ModelSerializer(Serializer):
601601
_related_class = PrimaryKeyRelatedField
602602

603603
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+
"""
604624
# Check that the user isn't trying to handle a writable nested field.
605625
# If we don't do this explicitly they'd likely get a confusing
606626
# error at the point of calling `Model.objects.create()`.
@@ -651,14 +671,19 @@ def update(self, instance, validated_attrs):
651671
return instance
652672

653673
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
654682
field_names = set([
655683
field.source for field in self.fields.values()
656684
if (field.source != '*') and ('.' not in field.source)
657685
])
658686

659-
validators = getattr(getattr(self, 'Meta', None), 'validators', [])
660-
model_class = self.Meta.model
661-
662687
# Note that we make sure to check `unique_together` both on the
663688
# base model class, but also on any parent classes.
664689
for parent_class in [model_class] + list(model_class._meta.parents.keys()):

0 commit comments

Comments
 (0)