mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 11:33:59 +03:00
Choice fields from ModelSerializer.
This commit is contained in:
parent
365d20652e
commit
32d602880f
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user