On Feb 28, 10:50 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
>
> 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()))

Thanks for the code example, setattr() was exactly what I was looking
for!

I did have to make a minor change; I initially got this error:

    for k, v in json_obj.values():
ValueError: too many values to unpack

but was OK once I made the change to:

for k, v in json_obj.iteritems():
        setattr(self, k, v)

Thanks also for the hint to look at TypeDecorator and descriptors.

I was surprised to find that just passing the default attribute types/
values of "comment" after the setattr() calls via "session.save
(comment)" actually worked, since the "postdate" attribute on the
"comment" class instance had a type of "unicode". I was expecting to
have to handle changing this to a datetime type in order to get it to
work.

Am I right in guessing that this works because the underlying database
(PostgreSQL) recognized the unicode date string and was able to
convert it, or is SA doing something for me with its internal type
engine/system in conjunction with the db (i.e. in other words, I'm
trying to figure out what component is making this work so I know
where to look for what's considered a legal/well-formed/importable
javascript date string)?

Thanks again for all of the help, -e
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to