On Apr 11, 2008, at 11:45 AM, [EMAIL PROTECTED] wrote:
> > Hello All, > > I have a session object, for instance: > my_object = session.query(Person).filter_by(name='MYSELF').first() > > I know I can access to its attributes to modify the database: > my_object.name = 'YOURSELF' > my_object.town = 'PARIS' > > Is there a way to access its attributes on another way ? > > The point is that I have a dictionary that records all values of > several textcontrols (with wxPython): > my_dict = {'name':'YOURSELF', 'town':'PARIS'} etc > > I would like to link (with a loop) the keys of this dictionary with > the attributes of the session object, without having to > write again all the attributes (my_object.name = my_dict['name'] etc ) > since there are a lot. > > I tried to loop like this without success: > for (key,value) in my_dict.iteritems(): > my_object.key = value # my_object[key] = value doesn't > work either > > Thanks in advance for any hints and sorry for the low level of my > question ! Python provides the "setattr()" function to assign object attributes by string expression: for k, v in dict.iteritems(): setattr(obj, k, v) There is also the __dict__ attribute present on objects which can also be written to in the general case, but in SQLAlchemy we need you to use the attribute's "public" interface, i.e. setattr(), in order for SQLAlchemy's change events to take place. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---