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):
|
||||
queryset = model_field.rel.to._default_manager
|
||||
if to_many:
|
||||
return return serializers.ManyRelatedField(queryset=queryset)
|
||||
return serializers.ManyRelatedField(queryset=queryset)
|
||||
return serializers.RelatedField(queryset=queryset)
|
||||
|
||||
def get_field(self, model_field):
|
||||
|
|
|
@ -48,6 +48,7 @@ The following people have helped make REST framework great.
|
|||
* Max Hurl - [maximilianhurl]
|
||||
* Tomi Pajunen - [eofs]
|
||||
* Rob Dobson - [rdobson]
|
||||
* Daniel Vaca Araujo - [diviei]
|
||||
|
||||
Many thanks to everyone who's contributed to the project.
|
||||
|
||||
|
@ -127,3 +128,4 @@ To contact the author directly:
|
|||
[maximilianhurl]: https://github.com/maximilianhurl
|
||||
[eofs]: https://github.com/eofs
|
||||
[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.
|
||||
"""
|
||||
kwargs = {}
|
||||
if model_field.has_default():
|
||||
kwargs['required'] = False
|
||||
|
||||
field_mapping = {
|
||||
models.FloatField: FloatField,
|
||||
models.IntegerField: IntegerField,
|
||||
|
@ -421,14 +425,9 @@ class ModelSerializer(Serializer):
|
|||
models.BooleanField: BooleanField,
|
||||
}
|
||||
try:
|
||||
ret = field_mapping[model_field.__class__]()
|
||||
return field_mapping[model_field.__class__](**kwargs)
|
||||
except KeyError:
|
||||
ret = ModelField(model_field=model_field)
|
||||
|
||||
if model_field.default is not None:
|
||||
ret.required = False
|
||||
|
||||
return ret
|
||||
return ModelField(model_field=model_field, **kwargs)
|
||||
|
||||
def restore_object(self, attrs, instance=None):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user