ModelSerializer.get_field() now returns a ChoiceField if the model has choices.

This commit is contained in:
Marko Tibold 2012-10-22 21:38:18 +02:00
parent d1e05ea8d4
commit 91ea57bf98

View File

@ -391,10 +391,14 @@ class ModelSerializer(Serializer):
models.CommaSeparatedIntegerField: CharField,
models.BooleanField: BooleanField,
}
try:
ret = field_mapping[model_field.__class__]()
except KeyError:
ret = ModelField(model_field=model_field)
if model_field.flatchoices: # This ModelField contains choices
ret = ChoiceField(model_field.flatchoices)
else:
try:
ret = field_mapping[model_field.__class__]()
except KeyError:
ret = ModelField(model_field=model_field)
if model_field.default is not None:
ret.required = False