Skip to content

Instantly share code, notes, and snippets.

@craiglabenz
Created May 17, 2013 13:16
Show Gist options
  • Save craiglabenz/5598963 to your computer and use it in GitHub Desktop.
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.
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