Skip to content

Commit 5f133c3

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

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

rest_framework/utils/field_mapping.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ 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 (
95+
isinstance(model_field, models.TextField) and not model_field.choices or
96+
(postgres_fields and isinstance(model_field, postgres_fields.JSONField))
97+
):
9598
kwargs['style'] = {'base_template': 'textarea.html'}
9699

97100
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,5 +1,6 @@
11
from unittest import mock
22

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

@@ -8,6 +9,7 @@
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy