Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #24604 -- Added JSONField to contrib.postgres.
- Loading branch information
Showing
11 changed files
with
540 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .array import * # NOQA | ||
from .hstore import * # NOQA | ||
from .jsonb import * # NOQA | ||
from .ranges import * # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import json | ||
|
||
from psycopg2.extras import Json | ||
|
||
from django.contrib.postgres import forms, lookups | ||
from django.core import exceptions | ||
from django.db.models import Field, Transform | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
__all__ = ['JSONField'] | ||
|
||
|
||
class JSONField(Field): | ||
empty_strings_allowed = False | ||
description = _('A JSON object') | ||
default_error_messages = { | ||
'invalid': _("Value must be valid JSON."), | ||
} | ||
|
||
def db_type(self, connection): | ||
return 'jsonb' | ||
|
||
def get_transform(self, name): | ||
transform = super(JSONField, self).get_transform(name) | ||
if transform: | ||
return transform | ||
return KeyTransformFactory(name) | ||
|
||
def get_prep_value(self, value): | ||
if value is not None: | ||
return Json(value) | ||
return value | ||
|
||
def get_prep_lookup(self, lookup_type, value): | ||
if lookup_type in ('has_key', 'has_keys', 'has_any_keys'): | ||
return value | ||
if isinstance(value, (dict, list)): | ||
return Json(value) | ||
return super(JSONField, self).get_prep_lookup(lookup_type, value) | ||
|
||
def validate(self, value, model_instance): | ||
super(JSONField, self).validate(value, model_instance) | ||
try: | ||
json.dumps(value) | ||
except TypeError: | ||
raise exceptions.ValidationError( | ||
self.error_messages['invalid'], | ||
code='invalid', | ||
params={'value': value}, | ||
) | ||
|
||
def value_to_string(self, obj): | ||
value = self._get_val_from_obj(obj) | ||
return value | ||
|
||
def formfield(self, **kwargs): | ||
defaults = {'form_class': forms.JSONField} | ||
defaults.update(kwargs) | ||
return super(JSONField, self).formfield(**defaults) | ||
|
||
|
||
JSONField.register_lookup(lookups.DataContains) | ||
JSONField.register_lookup(lookups.ContainedBy) | ||
JSONField.register_lookup(lookups.HasKey) | ||
JSONField.register_lookup(lookups.HasKeys) | ||
JSONField.register_lookup(lookups.HasAnyKeys) | ||
|
||
|
||
class KeyTransform(Transform): | ||
|
||
def __init__(self, key_name, *args, **kwargs): | ||
super(KeyTransform, self).__init__(*args, **kwargs) | ||
self.key_name = key_name | ||
|
||
def as_sql(self, compiler, connection): | ||
key_transforms = [self.key_name] | ||
previous = self.lhs | ||
while isinstance(previous, KeyTransform): | ||
key_transforms.insert(0, previous.key_name) | ||
previous = previous.lhs | ||
lhs, params = compiler.compile(previous) | ||
if len(key_transforms) > 1: | ||
return "{} #> %s".format(lhs), [key_transforms] + params | ||
try: | ||
int(self.key_name) | ||
except ValueError: | ||
lookup = "'%s'" % self.key_name | ||
else: | ||
lookup = "%s" % self.key_name | ||
return "%s -> %s" % (lhs, lookup), params | ||
|
||
|
||
class KeyTransformFactory(object): | ||
|
||
def __init__(self, key_name): | ||
self.key_name = key_name | ||
|
||
def __call__(self, *args, **kwargs): | ||
return KeyTransform(self.key_name, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .array import * # NOQA | ||
from .hstore import * # NOQA | ||
from .jsonb import * # NOQA | ||
from .ranges import * # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
|
||
from django import forms | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
__all__ = ['JSONField'] | ||
|
||
|
||
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) | ||
|
||
def to_python(self, value): | ||
if value in self.empty_values: | ||
return None | ||
try: | ||
return json.loads(value) | ||
except ValueError: | ||
raise forms.ValidationError( | ||
self.error_messages['invalid'], | ||
code='invalid', | ||
params={'value': value}, | ||
) | ||
|
||
def prepare_value(self, value): | ||
return json.dumps(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.