Skip to content

Commit 4d9f9eb

Browse files
hramezanirpkilby
authored andcommitted
Changed default widget for TextField with choices to select (encode#6892)
1 parent dff9759 commit 4d9f9eb

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-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_model_serializer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class FieldOptionsModel(models.Model):
8989
default_field = models.IntegerField(default=0)
9090
descriptive_field = models.IntegerField(help_text='Some help text', verbose_name='A label')
9191
choices_field = models.CharField(max_length=100, choices=COLOR_CHOICES)
92+
text_choices_field = models.TextField(choices=COLOR_CHOICES)
9293

9394

9495
class ChoicesModel(models.Model):
@@ -211,6 +212,7 @@ class Meta:
211212
default_field = IntegerField(required=False)
212213
descriptive_field = IntegerField(help_text='Some help text', label='A label')
213214
choices_field = ChoiceField(choices=(('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')))
215+
text_choices_field = ChoiceField(choices=(('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')))
214216
""")
215217
self.assertEqual(repr(TestSerializer()), expected)
216218

0 commit comments

Comments
 (0)