[1.10.x] Fixed #26949 -- Fixed crash of disabled forms.JSONField.

Backport of 68de48c963 from master
This commit is contained in:
Olexander Yermakov 2016-07-26 08:18:08 -04:00 committed by Tim Graham
parent 3744fc1666
commit 714e287d9a
2 changed files with 10 additions and 0 deletions

View File

@ -21,6 +21,8 @@ class JSONField(forms.CharField):
super(JSONField, self).__init__(**kwargs)
def to_python(self, value):
if self.disabled:
return value
if value in self.empty_values:
return None
try:

View File

@ -260,6 +260,14 @@ class TestFormField(PostgreSQLTestCase):
form_field = model_field.formfield()
self.assertIsInstance(form_field, forms.JSONField)
def test_formfield_disabled(self):
class JsonForm(Form):
name = CharField()
jfield = forms.JSONField(disabled=True)
form = JsonForm({'name': 'xyz', 'jfield': '["bar"]'}, initial={'jfield': ['foo']})
self.assertIn('[&quot;foo&quot;]</textarea>', form.as_p())
def test_prepare_value(self):
field = forms.JSONField()
self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')