mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Refs #27003 -- Fixed JSONField crash on converted values.
This commit is contained in:
committed by
Tim Graham
parent
6573274161
commit
eed6150009
@@ -11,6 +11,10 @@ class InvalidJSONInput(six.text_type):
|
||||
pass
|
||||
|
||||
|
||||
class JSONString(six.text_type):
|
||||
pass
|
||||
|
||||
|
||||
class JSONField(forms.CharField):
|
||||
default_error_messages = {
|
||||
'invalid': _("'%(value)s' value must be valid JSON."),
|
||||
@@ -22,14 +26,20 @@ class JSONField(forms.CharField):
|
||||
return value
|
||||
if value in self.empty_values:
|
||||
return None
|
||||
elif isinstance(value, (list, dict, int, float, JSONString)):
|
||||
return value
|
||||
try:
|
||||
return json.loads(value)
|
||||
converted = json.loads(value)
|
||||
except ValueError:
|
||||
raise forms.ValidationError(
|
||||
self.error_messages['invalid'],
|
||||
code='invalid',
|
||||
params={'value': value},
|
||||
)
|
||||
if isinstance(converted, six.text_type):
|
||||
return JSONString(converted)
|
||||
else:
|
||||
return converted
|
||||
|
||||
def bound_data(self, data, initial):
|
||||
if self.disabled:
|
||||
|
||||
Reference in New Issue
Block a user