Re: [sqlalchemy] How to map to read only descriptor
On Jun 11, 2010, at 5:16 AM, jpeck wrote: > I am trying to map to a simple read only property. According to the > docs, I *think* I am supposed to use synonym. The problem is that I am > getting a None value for the mapped descriptor's column. > > For example: > > import datetime > from sqlalchemy import Column, Table, Integer, String, MetaData, > create_engine > from sqlalchemy.orm import mapper, sessionmaker, synonym > > meta = MetaData() > foo_table = Table('foo', meta, >Column('id', String(3), primary_key=True), >Column('description', String(64), nullable=False), >Column('calculated_value', Integer, nullable=False), >) > > class Foo(object): >def __init__(self, id, description): >self.id = id >self.description = description > >@property >def calculated_value(self): >self._calculated_value = datetime.datetime.now().second + 10 >return self._calculated_value > > mapper(Foo, foo_table, >properties = { >'calculated_value' : synonym('_calculated_value', > map_column=True) >} > ) wouldn't this be simpler ? class Foo(object): def __init__(self, id, description): self.id = id self.description = description self.calculated_value = datetime.now().second + 10 mapper(Foo, foo_table) want it read only ? class Foo(object): def __init__(self, id, description): self.id = id self.description = description self._calculated_value = datetime.now().second + 10 @property def calculated_value(self): return self._calculated_value mapper(Foo, foo_table, properties={'_calculated_value':foo_table.c.calculated_value}) another option is to stick "datetime.now().second + 10" into theTable directly as a "default" for the Column. If it were still empty at flush time, it would get persisted at that point. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.
Re: [sqlalchemy] How to map to read only descriptor
On Fri, Jun 11, 2010 at 4:16 AM, jpeck wrote: > I am trying to map to a simple read only property. According to the > docs, I *think* I am supposed to use synonym. The problem is that I am > getting a None value for the mapped descriptor's column. > Ok, so I got this working with synonym, but I am not sure I am doing this the best way. As long as I access the descriptor before an insert or update, the synonym worked ok. I ended up using a MapperExtension like so: class UpdatePropertiesExtension(MapperExtension): def __init__(self, properties): self.properties = properties def _update_properties(self, instance): for p in self.properties: getattr(instance, p) def before_insert(self, mapper, connection, instance): self._update_properties(instance) def before_update(self, mapper, connection, instance): self._update_properties(instance) This works fine for me, but I'm wondering if there is a better way to do this? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.
[sqlalchemy] How to map to read only descriptor
I am trying to map to a simple read only property. According to the docs, I *think* I am supposed to use synonym. The problem is that I am getting a None value for the mapped descriptor's column. For example: import datetime from sqlalchemy import Column, Table, Integer, String, MetaData, create_engine from sqlalchemy.orm import mapper, sessionmaker, synonym meta = MetaData() foo_table = Table('foo', meta, Column('id', String(3), primary_key=True), Column('description', String(64), nullable=False), Column('calculated_value', Integer, nullable=False), ) class Foo(object): def __init__(self, id, description): self.id = id self.description = description @property def calculated_value(self): self._calculated_value = datetime.datetime.now().second + 10 return self._calculated_value mapper(Foo, foo_table, properties = { 'calculated_value' : synonym('_calculated_value', map_column=True) } ) engine = create_engine('sqlite:///test.db') Session = sessionmaker(bind=engine) meta.create_all(bind=engine) session = Session() session.add( Foo('xyz', 'test only') ) session.commit() Since self._calculated_value is created within the property, it does not exist until you actually access the property. How do I set this up to get sqlalchemy to go use the actual return value of the property when session.commit is called? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.