Created
April 25, 2013 21:23
-
-
Save rouge8/5463281 to your computer and use it in GitHub Desktop.
Django REST Framework DictField
This file contains hidden or 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
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