summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/forms
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2016-03-26 20:11:57 +0100
committerClaude Paroz <claude@2xlibre.net>2016-04-01 09:04:20 +0200
commitdb19619545dd99a1d2502c72974d79eca33acff7 (patch)
tree114ee5c154d19d49d06d4f6e9e1deb4e2d4e7039 /django/contrib/postgres/forms
parent64aba7a8aba06b8be52a1a099b44e1d3be4bdd26 (diff)
Fixed #25532 -- Properly redisplayed JSONField form input values
Thanks David Szotten for the report and Tommy Beadle for code inspiration. Thanks Tim Graham for the review.
Diffstat (limited to 'django/contrib/postgres/forms')
-rw-r--r--django/contrib/postgres/forms/jsonb.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/django/contrib/postgres/forms/jsonb.py b/django/contrib/postgres/forms/jsonb.py
index 8eefc14993..415288dc90 100644
--- a/django/contrib/postgres/forms/jsonb.py
+++ b/django/contrib/postgres/forms/jsonb.py
@@ -1,11 +1,16 @@
import json
from django import forms
+from django.utils import six
from django.utils.translation import ugettext_lazy as _
__all__ = ['JSONField']
+class InvalidJSONInput(six.text_type):
+ pass
+
+
class JSONField(forms.CharField):
default_error_messages = {
'invalid': _("'%(value)s' value must be valid JSON."),
@@ -27,5 +32,15 @@ class JSONField(forms.CharField):
params={'value': value},
)
+ def bound_data(self, data, initial):
+ if self.disabled:
+ return initial
+ try:
+ return json.loads(data)
+ except ValueError:
+ return InvalidJSONInput(data)
+
def prepare_value(self, value):
+ if isinstance(value, InvalidJSONInput):
+ return value
return json.dumps(value)