From 714e287d9a2b8ca7668a951e7193d9bbae3c3162 Mon Sep 17 00:00:00 2001 From: Olexander Yermakov Date: Tue, 26 Jul 2016 08:18:08 -0400 Subject: [PATCH] [1.10.x] Fixed #26949 -- Fixed crash of disabled forms.JSONField. Backport of 68de48c96328e13d5dbdb1f3006e4a1ca74f3c34 from master --- django/contrib/postgres/forms/jsonb.py | 2 ++ tests/postgres_tests/test_json.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/django/contrib/postgres/forms/jsonb.py b/django/contrib/postgres/forms/jsonb.py index 415288dc90..df2504cc98 100644 --- a/django/contrib/postgres/forms/jsonb.py +++ b/django/contrib/postgres/forms/jsonb.py @@ -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: diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py index d4f0a9bb57..1978552cf9 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -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('["foo"]', form.as_p()) + def test_prepare_value(self): field = forms.JSONField() self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')