summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Denton <charleswdenton@gmail.com>2016-06-13 13:09:54 +0100
committerTim Graham <timograham@gmail.com>2016-06-13 08:09:54 -0400
commitf2c0eb19e961f5864573251e70bdcdecd0250aed (patch)
treefd6aaa4878bebe72b1a56f2fdbc1fda9474a6b5b
parent57eb17b8c7d106c9a3264aa35b8dabb179a1e17b (diff)
Fixed #26748 -- Allowed overriding JSONField's widget with an attribute.
-rw-r--r--django/contrib/postgres/forms/jsonb.py5
-rw-r--r--tests/postgres_tests/test_json.py20
2 files changed, 20 insertions, 5 deletions
diff --git a/django/contrib/postgres/forms/jsonb.py b/django/contrib/postgres/forms/jsonb.py
index 415288dc90..bd1f175e4b 100644
--- a/django/contrib/postgres/forms/jsonb.py
+++ b/django/contrib/postgres/forms/jsonb.py
@@ -15,10 +15,7 @@ class JSONField(forms.CharField):
default_error_messages = {
'invalid': _("'%(value)s' value must be valid JSON."),
}
-
- def __init__(self, **kwargs):
- kwargs.setdefault('widget', forms.Textarea)
- super(JSONField, self).__init__(**kwargs)
+ widget = forms.Textarea
def to_python(self, value):
if value in self.empty_values:
diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py
index 11b6a349aa..f334118a1f 100644
--- a/tests/postgres_tests/test_json.py
+++ b/tests/postgres_tests/test_json.py
@@ -3,7 +3,7 @@ import unittest
from django.core import exceptions, serializers
from django.db import connection
-from django.forms import CharField, Form
+from django.forms import CharField, Form, widgets
from django.test import TestCase
from django.utils.html import escape
@@ -291,3 +291,21 @@ class TestFormField(PostgreSQLTestCase):
form = JsonForm({'name': 'xy', 'jfield': '{"foo"}'})
# Appears once in the textarea and once in the error message
self.assertEqual(form.as_p().count(escape('{"foo"}')), 2)
+
+ def test_widget(self):
+ """The default widget of a JSONField is a Textarea."""
+ field = forms.JSONField()
+ self.assertIsInstance(field.widget, widgets.Textarea)
+
+ def test_custom_widget_kwarg(self):
+ """The widget can be overridden with a kwarg."""
+ field = forms.JSONField(widget=widgets.Input)
+ self.assertIsInstance(field.widget, widgets.Input)
+
+ def test_custom_widget_attribute(self):
+ """The widget can be overridden with an attribute."""
+ class CustomJSONField(forms.JSONField):
+ widget = widgets.Input
+
+ field = CustomJSONField()
+ self.assertIsInstance(field.widget, widgets.Input)