Skip to content

Instantly share code, notes, and snippets.

@rouge8
Created April 25, 2013 21:23
Show Gist options
  • Save rouge8/5463281 to your computer and use it in GitHub Desktop.
Save rouge8/5463281 to your computer and use it in GitHub Desktop.
Django REST Framework DictField
from ast import literal_eval
class DictField(serializers.WritableField):
type_name = 'DictField'
def from_native(self, value):
if isinstance(value, basestring):
try:
value = literal_eval(value)
except SyntaxError:
value = value
if isinstance(value, dict):
return value
else:
raise serializers.ValidationError('Value is not a dictionary.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment