summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/forms
diff options
context:
space:
mode:
authorsage <laymonage@gmail.com>2019-06-09 07:56:37 +0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-05-08 07:23:31 +0200
commit6789ded0a6ab797f0dcdfa6ad5d1cfa46e23abcd (patch)
tree1de598fc92480c64835b60b6ddbb461c3cd2e864 /django/contrib/postgres/forms
parentf97f71f59249f1fbeebe84d4fc858d70fc456f7d (diff)
Fixed #12990, Refs #27694 -- Added JSONField model field.
Thanks to Adam Johnson, Carlton Gibson, Mariusz Felisiak, and Raphael Michel for mentoring this Google Summer of Code 2019 project and everyone else who helped with the patch. Special thanks to Mads Jensen, Nick Pope, and Simon Charette for extensive reviews. Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'django/contrib/postgres/forms')
-rw-r--r--django/contrib/postgres/forms/jsonb.py69
1 files changed, 11 insertions, 58 deletions
diff --git a/django/contrib/postgres/forms/jsonb.py b/django/contrib/postgres/forms/jsonb.py
index 196d2b9096..ebc85efa6f 100644
--- a/django/contrib/postgres/forms/jsonb.py
+++ b/django/contrib/postgres/forms/jsonb.py
@@ -1,63 +1,16 @@
-import json
+import warnings
-from django import forms
-from django.core.exceptions import ValidationError
-from django.utils.translation import gettext_lazy as _
+from django.forms import JSONField as BuiltinJSONField
+from django.utils.deprecation import RemovedInDjango40Warning
__all__ = ['JSONField']
-class InvalidJSONInput(str):
- pass
-
-
-class JSONString(str):
- pass
-
-
-class JSONField(forms.CharField):
- default_error_messages = {
- 'invalid': _('ā€œ%(value)sā€ value must be valid JSON.'),
- }
- widget = forms.Textarea
-
- def to_python(self, value):
- if self.disabled:
- return value
- if value in self.empty_values:
- return None
- elif isinstance(value, (list, dict, int, float, JSONString)):
- return value
- try:
- converted = json.loads(value)
- except json.JSONDecodeError:
- raise ValidationError(
- self.error_messages['invalid'],
- code='invalid',
- params={'value': value},
- )
- if isinstance(converted, str):
- return JSONString(converted)
- else:
- return converted
-
- def bound_data(self, data, initial):
- if self.disabled:
- return initial
- try:
- return json.loads(data)
- except json.JSONDecodeError:
- return InvalidJSONInput(data)
-
- def prepare_value(self, value):
- if isinstance(value, InvalidJSONInput):
- return value
- return json.dumps(value)
-
- def has_changed(self, initial, data):
- if super().has_changed(initial, data):
- return True
- # For purposes of seeing whether something has changed, True isn't the
- # same as 1 and the order of keys doesn't matter.
- data = self.to_python(data)
- return json.dumps(initial, sort_keys=True) != json.dumps(data, sort_keys=True)
+class JSONField(BuiltinJSONField):
+ def __init__(self, *args, **kwargs):
+ warnings.warn(
+ 'django.contrib.postgres.forms.JSONField is deprecated in favor '
+ 'of django.forms.JSONField.',
+ RemovedInDjango40Warning, stacklevel=2,
+ )
+ super().__init__(*args, **kwargs)