Created
May 17, 2013 13:16
-
-
Save craiglabenz/5598963 to your computer and use it in GitHub Desktop.
Django provides nice model serializers, but if their structure isn't exactly what you want then use this template to serialize them yourself.
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
def serialize(self): | |
obj = {} | |
for field in self._meta.fields: | |
value = field.value_from_object(self) | |
if isinstance(value, datetime.datetime): | |
obj[field.name] = field.value_to_string(self) | |
else: | |
obj[field.name] = value | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment