Fixed #28319 -- Made TextField with choices use a Select widget.
This commit is contained in:
parent
3b050fd0d0
commit
dc63ad7ac0
|
@ -2109,7 +2109,9 @@ class TextField(Field):
|
||||||
# Passing max_length to forms.CharField means that the value's length
|
# Passing max_length to forms.CharField means that the value's length
|
||||||
# will be validated twice. This is considered acceptable since we want
|
# will be validated twice. This is considered acceptable since we want
|
||||||
# the value in the form field (to pass into widget for example).
|
# the value in the form field (to pass into widget for example).
|
||||||
defaults = {'max_length': self.max_length, 'widget': forms.Textarea}
|
defaults = {'max_length': self.max_length}
|
||||||
|
if not self.choices:
|
||||||
|
defaults['widget'] = forms.Textarea
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
return super().formfield(**defaults)
|
return super().formfield(**defaults)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from unittest import skipIf
|
from unittest import skipIf
|
||||||
|
|
||||||
|
from django import forms
|
||||||
from django.db import connection, models
|
from django.db import connection, models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
@ -18,6 +19,11 @@ class TextFieldTests(TestCase):
|
||||||
self.assertIsNone(tf1.formfield().max_length)
|
self.assertIsNone(tf1.formfield().max_length)
|
||||||
self.assertEqual(2345, tf2.formfield().max_length)
|
self.assertEqual(2345, tf2.formfield().max_length)
|
||||||
|
|
||||||
|
def test_choices_generates_select_widget(self):
|
||||||
|
"""A TextField with choices uses a Select widget."""
|
||||||
|
f = models.TextField(choices=[('A', 'A'), ('B', 'B')])
|
||||||
|
self.assertIsInstance(f.formfield().widget, forms.Select)
|
||||||
|
|
||||||
def test_to_python(self):
|
def test_to_python(self):
|
||||||
"""TextField.to_python() should return a string."""
|
"""TextField.to_python() should return a string."""
|
||||||
f = models.TextField()
|
f = models.TextField()
|
||||||
|
|
Loading…
Reference in New Issue