mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Minor validtors tweak
This commit is contained in:
parent
22c5b863bc
commit
270c7acdd7
|
@ -727,10 +727,11 @@ The `UniqueTogetherValidator` should be applied to a serializer, and takes a `qu
|
||||||
position = serializers.IntegerField()
|
position = serializers.IntegerField()
|
||||||
name = serializers.CharField(max_length=100)
|
name = serializers.CharField(max_length=100)
|
||||||
|
|
||||||
default_validators = [UniqueTogetherValidator(
|
class Meta:
|
||||||
queryset=RaceResult.objects.all(),
|
validators = [UniqueTogetherValidator(
|
||||||
fields=('category', 'position')
|
queryset=RaceResult.objects.all(),
|
||||||
)]
|
fields=('category', 'position')
|
||||||
|
)]
|
||||||
|
|
||||||
#### The `UniqueForDateValidator` classes.
|
#### The `UniqueForDateValidator` classes.
|
||||||
|
|
||||||
|
|
|
@ -601,6 +601,26 @@ class ModelSerializer(Serializer):
|
||||||
_related_class = PrimaryKeyRelatedField
|
_related_class = PrimaryKeyRelatedField
|
||||||
|
|
||||||
def create(self, validated_attrs):
|
def create(self, validated_attrs):
|
||||||
|
"""
|
||||||
|
We have a bit of extra checking around this in order to provide
|
||||||
|
descriptive messages when something goes wrong, but this method is
|
||||||
|
essentially just:
|
||||||
|
|
||||||
|
return ExampleModel.objects.create(**validated_attrs)
|
||||||
|
|
||||||
|
If there are many to many fields present on the instance then they
|
||||||
|
cannot be set until the model is instantiated, in which case the
|
||||||
|
implementation is like so:
|
||||||
|
|
||||||
|
example_relationship = validated_attrs.pop('example_relationship')
|
||||||
|
instance = ExampleModel.objects.create(**validated_attrs)
|
||||||
|
instance.example_relationship = example_relationship
|
||||||
|
return instance
|
||||||
|
|
||||||
|
The default implementation also does not handle nested relationships.
|
||||||
|
If you want to support writable nested relationships you'll need
|
||||||
|
to write an explicit `.create()` method.
|
||||||
|
"""
|
||||||
# Check that the user isn't trying to handle a writable nested field.
|
# Check that the user isn't trying to handle a writable nested field.
|
||||||
# If we don't do this explicitly they'd likely get a confusing
|
# If we don't do this explicitly they'd likely get a confusing
|
||||||
# error at the point of calling `Model.objects.create()`.
|
# error at the point of calling `Model.objects.create()`.
|
||||||
|
@ -651,14 +671,19 @@ class ModelSerializer(Serializer):
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def get_validators(self):
|
def get_validators(self):
|
||||||
|
# If the validators have been declared explicitly then use that.
|
||||||
|
validators = getattr(getattr(self, 'Meta', None), 'validators', None)
|
||||||
|
if validators is not None:
|
||||||
|
return validators
|
||||||
|
|
||||||
|
# Determine the default set of validators.
|
||||||
|
validators = []
|
||||||
|
model_class = self.Meta.model
|
||||||
field_names = set([
|
field_names = set([
|
||||||
field.source for field in self.fields.values()
|
field.source for field in self.fields.values()
|
||||||
if (field.source != '*') and ('.' not in field.source)
|
if (field.source != '*') and ('.' not in field.source)
|
||||||
])
|
])
|
||||||
|
|
||||||
validators = getattr(getattr(self, 'Meta', None), 'validators', [])
|
|
||||||
model_class = self.Meta.model
|
|
||||||
|
|
||||||
# Note that we make sure to check `unique_together` both on the
|
# Note that we make sure to check `unique_together` both on the
|
||||||
# base model class, but also on any parent classes.
|
# base model class, but also on any parent classes.
|
||||||
for parent_class in [model_class] + list(model_class._meta.parents.keys()):
|
for parent_class in [model_class] + list(model_class._meta.parents.keys()):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user