Skip to content

Instantly share code, notes, and snippets.

View craiglabenz's full-sized avatar

Craig Labenz craiglabenz

View GitHub Profile
@craiglabenz
craiglabenz / django_model_serializer.py
Created May 17, 2013 13:16
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