mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-28 17:09:59 +03:00
Changed default widget for TextField with choices to select box.
This commit is contained in:
parent
ec1b14174f
commit
474145512a
|
@ -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:
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from unittest import mock
|
||||
|
||||
from django.conf.urls import url
|
||||
from django.db import models
|
||||
from django.test import TestCase, override_settings
|
||||
|
||||
from rest_framework.decorators import action
|
||||
|
@ -8,6 +9,7 @@ from rest_framework.routers import SimpleRouter
|
|||
from rest_framework.serializers import ModelSerializer
|
||||
from rest_framework.utils import json
|
||||
from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
||||
from rest_framework.utils.field_mapping import get_field_kwargs
|
||||
from rest_framework.utils.formatting import lazy_format
|
||||
from rest_framework.utils.urls import remove_query_param, replace_query_param
|
||||
from rest_framework.views import APIView
|
||||
|
@ -267,3 +269,18 @@ class LazyFormatTests(TestCase):
|
|||
assert message.format.call_count == 1
|
||||
str(formatted)
|
||||
assert message.format.call_count == 1
|
||||
|
||||
|
||||
class GetFieldKwargsTest(TestCase):
|
||||
def test_get_text_field_kwargs(self):
|
||||
# TextField without choices
|
||||
f = models.TextField()
|
||||
kwargs = get_field_kwargs('f', f)
|
||||
assert 'style' in kwargs
|
||||
assert 'choices' not in kwargs
|
||||
|
||||
# TextField with choices
|
||||
f = models.TextField(choices=['ONE', 'TWO'])
|
||||
kwargs = get_field_kwargs('f', f)
|
||||
assert 'style' not in kwargs
|
||||
assert 'choices' in kwargs
|
||||
|
|
Loading…
Reference in New Issue
Block a user