mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Merge branch 'restframework2' into split-generic-views
* restframework2: Fix ModelSerializer logic for fields with default value, which should have required=False set Added @diviei - Thanks! Fix typo reported by @diviei
This commit is contained in:
commit
16e249d60d
|
@ -254,7 +254,7 @@ When serializing objects using a nested representation any occurances of recursi
|
||||||
def get_related_field(self, model_field, to_many=False):
|
def get_related_field(self, model_field, to_many=False):
|
||||||
queryset = model_field.rel.to._default_manager
|
queryset = model_field.rel.to._default_manager
|
||||||
if to_many:
|
if to_many:
|
||||||
return return serializers.ManyRelatedField(queryset=queryset)
|
return serializers.ManyRelatedField(queryset=queryset)
|
||||||
return serializers.RelatedField(queryset=queryset)
|
return serializers.RelatedField(queryset=queryset)
|
||||||
|
|
||||||
def get_field(self, model_field):
|
def get_field(self, model_field):
|
||||||
|
|
|
@ -48,6 +48,7 @@ The following people have helped make REST framework great.
|
||||||
* Max Hurl - [maximilianhurl]
|
* Max Hurl - [maximilianhurl]
|
||||||
* Tomi Pajunen - [eofs]
|
* Tomi Pajunen - [eofs]
|
||||||
* Rob Dobson - [rdobson]
|
* Rob Dobson - [rdobson]
|
||||||
|
* Daniel Vaca Araujo - [diviei]
|
||||||
|
|
||||||
Many thanks to everyone who's contributed to the project.
|
Many thanks to everyone who's contributed to the project.
|
||||||
|
|
||||||
|
@ -127,3 +128,4 @@ To contact the author directly:
|
||||||
[maximilianhurl]: https://github.com/maximilianhurl
|
[maximilianhurl]: https://github.com/maximilianhurl
|
||||||
[eofs]: https://github.com/eofs
|
[eofs]: https://github.com/eofs
|
||||||
[rdobson]: https://github.com/rdobson
|
[rdobson]: https://github.com/rdobson
|
||||||
|
[diviei]: https://github.com/diviei
|
||||||
|
|
|
@ -406,6 +406,10 @@ class ModelSerializer(Serializer):
|
||||||
"""
|
"""
|
||||||
Creates a default instance of a basic non-relational field.
|
Creates a default instance of a basic non-relational field.
|
||||||
"""
|
"""
|
||||||
|
kwargs = {}
|
||||||
|
if model_field.has_default():
|
||||||
|
kwargs['required'] = False
|
||||||
|
|
||||||
field_mapping = {
|
field_mapping = {
|
||||||
models.FloatField: FloatField,
|
models.FloatField: FloatField,
|
||||||
models.IntegerField: IntegerField,
|
models.IntegerField: IntegerField,
|
||||||
|
@ -421,14 +425,9 @@ class ModelSerializer(Serializer):
|
||||||
models.BooleanField: BooleanField,
|
models.BooleanField: BooleanField,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
ret = field_mapping[model_field.__class__]()
|
return field_mapping[model_field.__class__](**kwargs)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
ret = ModelField(model_field=model_field)
|
return ModelField(model_field=model_field, **kwargs)
|
||||||
|
|
||||||
if model_field.default is not None:
|
|
||||||
ret.required = False
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def restore_object(self, attrs, instance=None):
|
def restore_object(self, attrs, instance=None):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user