[sqlalchemy] Re: Model to Dictionary

2007-07-25 Thread Jonathan Ellis

why not just pass model_instance.__dict__ ?

On 7/23/07, HiTekElvis [EMAIL PROTECTED] wrote:

 Anybody know a way to change a model into a dictionary?

 For those to whom it means anything, I'm hoping to pass that
 dictionary into a formencode.Schema.from_python method.

 Any ideas?

 -Josh


 


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Model to Dictionary

2007-07-24 Thread King Simon-NFHD78

What do you mean by a model? If you are talking about an instance of a
mapped class, you could try something like this (untested):

def model_to_dict(instance):
model_dict = {}
for propname in instance.mapper.props:
model_dict[propname] = getattr(instance, propname)
return model_dict

Note that if you have relations to other mapped classes this will put
instances of those other classes in the dict. If you don't want that,
you could check to see if the result of the getattr is a mapped class
and call the function recursively. But you'd probably also need to check
for circular references, and treat x-to-many relationships specially.

Simon

 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:[EMAIL PROTECTED] On Behalf Of HiTekElvis
 Sent: 23 July 2007 23:31
 To: sqlalchemy
 Subject: [sqlalchemy] Model to Dictionary
 
 
 Anybody know a way to change a model into a dictionary?
 
 For those to whom it means anything, I'm hoping to pass that
 dictionary into a formencode.Schema.from_python method.
 
 Any ideas?
 
 -Josh
 
 
  
 

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---