From 91ea57bf9847c948d96956c3925d45d6ed4f0ef7 Mon Sep 17 00:00:00 2001 From: Marko Tibold Date: Mon, 22 Oct 2012 21:38:18 +0200 Subject: [PATCH] ModelSerializer.get_field() now returns a ChoiceField if the model has choices. --- rest_framework/serializers.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 221cbf2fc..563b5520f 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -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