Skip to content

Instantly share code, notes, and snippets.

@llimllib
Created June 4, 2010 15:08
Show Gist options
  • Save llimllib/425521 to your computer and use it in GitHub Desktop.
Save llimllib/425521 to your computer and use it in GitHub Desktop.
#a very simple serialization
def _serialize(model_instance):
#the defaultdict returns an identity function by default
types = defaultdict(lambda: lambda x: x)
types[datetime] = lambda x: unicode(x)
r = {}
for f in model_instance._meta.fields:
attval = getattr(model_instance, f.attname)
#serialize the type if it needs serialization
attval = types[attval.__class__](attval)
r[f.attname] = attval
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment