Handle binary or unicode with JSONField

This commit is contained in:
Tom Christie 2015-09-28 17:47:51 +01:00
parent dad207de66
commit 265ec8ac62
2 changed files with 3 additions and 1 deletions

View File

@ -461,7 +461,7 @@ You can also use the declarative style, as with `ListField`. For example:
## JSONField
A field class that validates that the incoming data structure consists of valid JSON primitives. In its alternate binary mode, it will represent and validate JSON encoded strings.
A field class that validates that the incoming data structure consists of valid JSON primitives. In its alternate binary mode, it will represent and validate JSON-encoded binary strings.
**Signature**: `JSONField(binary)`

View File

@ -1535,6 +1535,8 @@ class JSONField(Field):
def to_internal_value(self, data):
try:
if self.binary:
if isinstance(data, six.binary_type):
data = data.decode('utf-8')
return json.loads(data)
else:
json.dumps(data)