Fixed #28555 -- Made CharField convert whitespace-only values to the empty_value when strip is enabled.
This commit is contained in:
parent
79ae5811c7
commit
48c394a6fc
|
@ -221,11 +221,12 @@ class CharField(Field):
|
|||
|
||||
def to_python(self, value):
|
||||
"""Return a string."""
|
||||
if value not in self.empty_values:
|
||||
value = str(value)
|
||||
if self.strip:
|
||||
value = value.strip()
|
||||
if value in self.empty_values:
|
||||
return self.empty_value
|
||||
value = str(value)
|
||||
if self.strip:
|
||||
value = value.strip()
|
||||
return value
|
||||
|
||||
def widget_attrs(self, widget):
|
||||
|
|
|
@ -120,6 +120,29 @@ class CharFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
|||
self.assertEqual(f.clean(' 1'), ' 1')
|
||||
self.assertEqual(f.clean('1 '), '1 ')
|
||||
|
||||
def test_strip_before_checking_empty(self):
|
||||
"""
|
||||
A whitespace-only value, ' ', is stripped to an empty string and then
|
||||
converted to the empty value, None.
|
||||
"""
|
||||
f = CharField(required=False, empty_value=None)
|
||||
self.assertIsNone(f.clean(' '))
|
||||
|
||||
def test_clean_non_string(self):
|
||||
"""CharField.clean() calls str(value) before stripping it."""
|
||||
class StringWrapper:
|
||||
def __init__(self, v):
|
||||
self.v = v
|
||||
|
||||
def __str__(self):
|
||||
return self.v
|
||||
|
||||
value = StringWrapper(' ')
|
||||
f1 = CharField(required=False, empty_value=None)
|
||||
self.assertIsNone(f1.clean(value))
|
||||
f2 = CharField(strip=False)
|
||||
self.assertEqual(f2.clean(value), ' ')
|
||||
|
||||
def test_charfield_disabled(self):
|
||||
f = CharField(disabled=True)
|
||||
self.assertWidgetRendersTo(f, '<input type="text" name="f" id="id_f" disabled required />')
|
||||
|
|
Loading…
Reference in New Issue