On Feb 27, 2009, at 11:24 PM, eLuke wrote:
> I have a simple model/class named "Comment" with columns "id" (int), > "comment" (text), and "postdate" (datetime). > > Below are the main bits of the python code I'm using to test how I > should handle this: > > # json from the client's browser > json = r'{"id":1,"postdate":"Sat Jan 31 2009 22:18:15 GMT-0500 > (Eastern Standard Time)","comment":"Comment text."}' > # create comment instance > comment = model.Comment() > # decode json to python object (dict) > json_obj = simplejson.loads(json) > # ??? > > So now I have "json_obj" as a python dict object with key/value > pairs... and I need to somehow get this into: > > comment.id > comment.comment > comment.postdate > > I've searched and searched and have only found one good example piece > of code that I may be able to use to at least handle the parsing/ > generation of the datetime type from a javascript Date string; but, > I'm still stuck on how to iterate over the dict keys and do an > assignment to the properties in the "comment" object instance in a way > that's reusable for other model classes. A method like this would work: def from_json(self, json): json_obj = simplejson.loads(json) for k, v in json_obj.values(): setattr(self, k, v) if you're concerned about dates you can use a date-based TypeDecorator which can receive a fully qualified datestring as an argument. Or use descriptors on your mapped classes (i.e. date = property(def get_date()/def set_date())) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---