Michael Bayer wrote: > also your example should read like this: > > a = session.query(A).all()[0] > print a.time_units > a.time_units = 1 > print a.time_units > #print A.timeunits > #print A.time_units > Huh ? time_units is the column name in the database. I want to refer to that column through the attribute 'timeunits'. I also want to use descriptors to do extra work when setting that attribute. The synonym api docs say:
*def synonym(/name/, /map_column=False/, /descriptor=None/, /proxy=False/)* Set up name as a synonym to another mapped property. So when I say: mapper(A, table_A, properties = { "time_units" : synonym("timeunits", map_column = True) }) Am I not saying 'create an alias to column 'time_units' in table_A and call it 'timeunits' ?? Incidentally.. creating this mapping replaces the existing property/data descriptor class variable with an instance of a Mapper: print A.timeunits mapper(A, table_A, properties = { "time_units" : synonym("timeunits", map_column = True) }) print A.timeunits >> ./test.py <property object at 0x848c3ec> <Mapper at 0x84e5d4c; A>.timeunits Unless Mapper takes special care of any existing descriptors how can they co-exist with mapped attributes ? -- t o b e > the A.time_units is the class-bound descriptor so that raises an > exception due to a missing __str__() method. this is a small bug but > does not break the functionality you're looking for. The in-python > access to the attribute is performed via the attribute you've created, > i.e. a.time_units. > > > On Jan 26, 2009, at 12:59 PM, Toby Bradshaw wrote: > >> CREATE TABLE example_a >> ( >> id integer NOT NULL, >> time_units integer, >> CONSTRAINT example_a_pkey PRIMARY KEY (id) >> ) >> >> and (based on >> http://www.sqlalchemy.org/docs/05/mappers.html#using-descriptors) >> <http://www.sqlalchemy.org/docs/05/mappers.html#using-descriptors%29>: >> >> from sqlalchemy import * >> from sqlalchemy.orm import sessionmaker, mapper, synonym >> >> class A(object): >> def _set_t(self, t): >> print "SET" >> self.timeunits = t / float(10) >> def _get_t(self): >> print "GET" >> return self.timeunits * 10 >> time_units = property(_get_t, _set_t) >> >> engine = create_engine("postgres://tobe:<snip>@localhost/test") >> engine.echo = False >> >> session_class = sessionmaker( >> bind = engine, autoflush = True, autocommit = False, >> ) >> >> meta = MetaData(bind = engine) >> table_A = Table("example_a", meta, autoload = True) >> >> print A.time_units >> mapper(A, table_A, properties = { >> "time_units" : synonym("timeunits", map_column = True) >> }) >> >> session = session_class() >> >> a = session.query(A).all()[0] >> print a.timeunits >> a.timeunits = 1 >> print a.timeunits >> print A.timeunits >> print A.time_units > > > > -- t o b e -- A truly clever developer will create code so easy to understand that a less than average developer could debug it. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---