Created
April 23, 2013 16:28
-
-
Save rouge8/5445149 to your computer and use it in GitHub Desktop.
Django REST Framework JSON Field
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
import json | |
from rest_framework import serializers | |
class JSONField(serializers.WritableField): | |
def to_native(self, obj): | |
return json.dumps(obj) | |
def from_native(self, value): | |
return json.loads(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd also maybe add a
try/catch
tofrom_native
, so that serialisation wouldn't fail pre-emptively, e.g.: