Changed default widget for TextField with choices to select (#6892)

This commit is contained in:
Hasan Ramezani 2019-12-04 21:24:49 +01:00 committed by Ryan P Kilby
parent dff9759555
commit 4d9f9eb192
2 changed files with 4 additions and 1 deletions

View File

@ -91,7 +91,8 @@ def get_field_kwargs(field_name, model_field):
if isinstance(model_field, models.SlugField):
kwargs['allow_unicode'] = model_field.allow_unicode
if isinstance(model_field, models.TextField) or (postgres_fields and isinstance(model_field, postgres_fields.JSONField)):
if isinstance(model_field, models.TextField) and not model_field.choices or \
(postgres_fields and isinstance(model_field, postgres_fields.JSONField)):
kwargs['style'] = {'base_template': 'textarea.html'}
if isinstance(model_field, models.AutoField) or not model_field.editable:

View File

@ -89,6 +89,7 @@ class FieldOptionsModel(models.Model):
default_field = models.IntegerField(default=0)
descriptive_field = models.IntegerField(help_text='Some help text', verbose_name='A label')
choices_field = models.CharField(max_length=100, choices=COLOR_CHOICES)
text_choices_field = models.TextField(choices=COLOR_CHOICES)
class ChoicesModel(models.Model):
@ -211,6 +212,7 @@ class TestRegularFieldMappings(TestCase):
default_field = IntegerField(required=False)
descriptive_field = IntegerField(help_text='Some help text', label='A label')
choices_field = ChoiceField(choices=(('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')))
text_choices_field = ChoiceField(choices=(('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')))
""")
self.assertEqual(repr(TestSerializer()), expected)