mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-18 12:30:58 +03:00
Merge branch 'master' of github.com:tomchristie/django-rest-framework
This commit is contained in:
commit
45dc44b203
|
@ -110,21 +110,21 @@ The first thing we need to get started on our Web API is to provide a way of ser
|
||||||
style = serializers.ChoiceField(choices=STYLE_CHOICES,
|
style = serializers.ChoiceField(choices=STYLE_CHOICES,
|
||||||
default='friendly')
|
default='friendly')
|
||||||
|
|
||||||
def create(self, validated_attrs):
|
def create(self, validated_data):
|
||||||
"""
|
"""
|
||||||
Create and return a new `Snippet` instance, given the validated data.
|
Create and return a new `Snippet` instance, given the validated data.
|
||||||
"""
|
"""
|
||||||
return Snippet.objects.create(**validated_attrs)
|
return Snippet.objects.create(**validated_data)
|
||||||
|
|
||||||
def update(self, instance, validated_attrs):
|
def update(self, instance, validated_data):
|
||||||
"""
|
"""
|
||||||
Update and return an existing `Snippet` instance, given the validated data.
|
Update and return an existing `Snippet` instance, given the validated data.
|
||||||
"""
|
"""
|
||||||
instance.title = validated_attrs.get('title', instance.title)
|
instance.title = validated_data.get('title', instance.title)
|
||||||
instance.code = validated_attrs.get('code', instance.code)
|
instance.code = validated_data.get('code', instance.code)
|
||||||
instance.linenos = validated_attrs.get('linenos', instance.linenos)
|
instance.linenos = validated_data.get('linenos', instance.linenos)
|
||||||
instance.language = validated_attrs.get('language', instance.language)
|
instance.language = validated_data.get('language', instance.language)
|
||||||
instance.style = validated_attrs.get('style', instance.style)
|
instance.style = validated_data.get('style', instance.style)
|
||||||
instance.save()
|
instance.save()
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
|
@ -374,6 +374,10 @@ class HTMLFormRenderer(BaseRenderer):
|
||||||
'base_template': 'input.html',
|
'base_template': 'input.html',
|
||||||
'input_type': 'time'
|
'input_type': 'time'
|
||||||
},
|
},
|
||||||
|
serializers.FileField: {
|
||||||
|
'base_template': 'input.html',
|
||||||
|
'input_type': 'file'
|
||||||
|
},
|
||||||
serializers.BooleanField: {
|
serializers.BooleanField: {
|
||||||
'base_template': 'checkbox.html'
|
'base_template': 'checkbox.html'
|
||||||
},
|
},
|
||||||
|
|
|
@ -608,20 +608,20 @@ class ModelSerializer(Serializer):
|
||||||
})
|
})
|
||||||
_related_class = PrimaryKeyRelatedField
|
_related_class = PrimaryKeyRelatedField
|
||||||
|
|
||||||
def create(self, validated_attrs):
|
def create(self, validated_data):
|
||||||
"""
|
"""
|
||||||
We have a bit of extra checking around this in order to provide
|
We have a bit of extra checking around this in order to provide
|
||||||
descriptive messages when something goes wrong, but this method is
|
descriptive messages when something goes wrong, but this method is
|
||||||
essentially just:
|
essentially just:
|
||||||
|
|
||||||
return ExampleModel.objects.create(**validated_attrs)
|
return ExampleModel.objects.create(**validated_data)
|
||||||
|
|
||||||
If there are many to many fields present on the instance then they
|
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
|
cannot be set until the model is instantiated, in which case the
|
||||||
implementation is like so:
|
implementation is like so:
|
||||||
|
|
||||||
example_relationship = validated_attrs.pop('example_relationship')
|
example_relationship = validated_data.pop('example_relationship')
|
||||||
instance = ExampleModel.objects.create(**validated_attrs)
|
instance = ExampleModel.objects.create(**validated_data)
|
||||||
instance.example_relationship = example_relationship
|
instance.example_relationship = example_relationship
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
@ -633,8 +633,8 @@ class ModelSerializer(Serializer):
|
||||||
# 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()`.
|
||||||
assert not any(
|
assert not any(
|
||||||
isinstance(field, BaseSerializer) and not field.read_only
|
isinstance(field, BaseSerializer) and (key in validated_attrs)
|
||||||
for field in self.fields.values()
|
for key, field in self.fields.items()
|
||||||
), (
|
), (
|
||||||
'The `.create()` method does not suport nested writable fields '
|
'The `.create()` method does not suport nested writable fields '
|
||||||
'by default. Write an explicit `.create()` method for serializer '
|
'by default. Write an explicit `.create()` method for serializer '
|
||||||
|
@ -644,17 +644,17 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
ModelClass = self.Meta.model
|
ModelClass = self.Meta.model
|
||||||
|
|
||||||
# Remove many-to-many relationships from validated_attrs.
|
# Remove many-to-many relationships from validated_data.
|
||||||
# They are not valid arguments to the default `.create()` method,
|
# They are not valid arguments to the default `.create()` method,
|
||||||
# as they require that the instance has already been saved.
|
# as they require that the instance has already been saved.
|
||||||
info = model_meta.get_field_info(ModelClass)
|
info = model_meta.get_field_info(ModelClass)
|
||||||
many_to_many = {}
|
many_to_many = {}
|
||||||
for field_name, relation_info in info.relations.items():
|
for field_name, relation_info in info.relations.items():
|
||||||
if relation_info.to_many and (field_name in validated_attrs):
|
if relation_info.to_many and (field_name in validated_data):
|
||||||
many_to_many[field_name] = validated_attrs.pop(field_name)
|
many_to_many[field_name] = validated_data.pop(field_name)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
instance = ModelClass.objects.create(**validated_attrs)
|
instance = ModelClass.objects.create(**validated_data)
|
||||||
except TypeError as exc:
|
except TypeError as exc:
|
||||||
msg = (
|
msg = (
|
||||||
'Got a `TypeError` when calling `%s.objects.create()`. '
|
'Got a `TypeError` when calling `%s.objects.create()`. '
|
||||||
|
@ -679,10 +679,10 @@ class ModelSerializer(Serializer):
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def update(self, instance, validated_attrs):
|
def update(self, instance, validated_data):
|
||||||
assert not any(
|
assert not any(
|
||||||
isinstance(field, BaseSerializer) and not field.read_only
|
isinstance(field, BaseSerializer) and (key in validated_attrs)
|
||||||
for field in self.fields.values()
|
for key, field in self.fields.items()
|
||||||
), (
|
), (
|
||||||
'The `.update()` method does not suport nested writable fields '
|
'The `.update()` method does not suport nested writable fields '
|
||||||
'by default. Write an explicit `.update()` method for serializer '
|
'by default. Write an explicit `.update()` method for serializer '
|
||||||
|
@ -690,7 +690,7 @@ class ModelSerializer(Serializer):
|
||||||
(self.__class__.__module__, self.__class__.__name__)
|
(self.__class__.__module__, self.__class__.__name__)
|
||||||
)
|
)
|
||||||
|
|
||||||
for attr, value in validated_attrs.items():
|
for attr, value in validated_data.items():
|
||||||
setattr(instance, attr, value)
|
setattr(instance, attr, value)
|
||||||
instance.save()
|
instance.save()
|
||||||
return instance
|
return instance
|
||||||
|
|
Loading…
Reference in New Issue
Block a user