Skip to content

Commit 4741455

Browse files
committed
Changed default widget for TextField with choices to select box.
1 parent ec1b141 commit 4741455

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

rest_framework/utils/field_mapping.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def get_field_kwargs(field_name, model_field):
9191
if isinstance(model_field, models.SlugField):
9292
kwargs['allow_unicode'] = model_field.allow_unicode
9393

94-
if isinstance(model_field, models.TextField) or (postgres_fields and isinstance(model_field, postgres_fields.JSONField)):
94+
if isinstance(model_field, models.TextField) and not model_field.choices or \
95+
(postgres_fields and isinstance(model_field, postgres_fields.JSONField)):
9596
kwargs['style'] = {'base_template': 'textarea.html'}
9697

9798
if isinstance(model_field, models.AutoField) or not model_field.editable:

tests/test_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from unittest import mock
22

33
from django.conf.urls import url
4+
from django.db import models
45
from django.test import TestCase, override_settings
56

67
from rest_framework.decorators import action
78
from rest_framework.routers import SimpleRouter
89
from rest_framework.serializers import ModelSerializer
910
from rest_framework.utils import json
1011
from rest_framework.utils.breadcrumbs import get_breadcrumbs
12+
from rest_framework.utils.field_mapping import get_field_kwargs
1113
from rest_framework.utils.formatting import lazy_format
1214
from rest_framework.utils.urls import remove_query_param, replace_query_param
1315
from rest_framework.views import APIView
@@ -267,3 +269,18 @@ def test_it_formats_lazily(self):
267269
assert message.format.call_count == 1
268270
str(formatted)
269271
assert message.format.call_count == 1
272+
273+
274+
class GetFieldKwargsTest(TestCase):
275+
def test_get_text_field_kwargs(self):
276+
# TextField without choices
277+
f = models.TextField()
278+
kwargs = get_field_kwargs('f', f)
279+
assert 'style' in kwargs
280+
assert 'choices' not in kwargs
281+
282+
# TextField with choices
283+
f = models.TextField(choices=['ONE', 'TWO'])
284+
kwargs = get_field_kwargs('f', f)
285+
assert 'style' not in kwargs
286+
assert 'choices' in kwargs

0 commit comments

Comments
 (0)