Choice fields from ModelSerializer.

This commit is contained in:
Tom Christie 2012-10-26 12:45:52 +01:00
parent 365d20652e
commit 32d602880f

View File

@ -3,6 +3,7 @@ import datetime
import types import types
from decimal import Decimal from decimal import Decimal
from django.db import models from django.db import models
from django.forms import widgets
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from rest_framework.compat import get_concrete_model from rest_framework.compat import get_concrete_model
from rest_framework.fields import * from rest_framework.fields import *
@ -409,6 +410,15 @@ class ModelSerializer(Serializer):
kwargs = {} kwargs = {}
if model_field.has_default(): if model_field.has_default():
kwargs['required'] = False kwargs['required'] = False
kwargs['default'] = model_field.default
if model_field.__class__ == models.TextField:
kwargs['widget'] = widgets.Textarea
# TODO: TypedChoiceField?
if model_field.flatchoices: # This ModelField contains choices
kwargs['choices'] = model_field.flatchoices
return ChoiceField(**kwargs)
field_mapping = { field_mapping = {
models.FloatField: FloatField, models.FloatField: FloatField,