Created
June 4, 2010 15:08
-
-
Save llimllib/425521 to your computer and use it in GitHub Desktop.
This file contains 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
#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