That's the first thing I tried, but validators don't get called unless you explicitly set a value to the column.
So for something like session.add(DeliveryAddress()), the validator doesn't get called. maanantai, 2. kesäkuuta 2014 14.27.47 UTC+3 Michael Bayer kirjoitti: > > > On Jun 2, 2014, at 1:47 AM, Alex Grönholm <alex.g...@nextday.fi > <javascript:>> wrote: > > > This has been a problem for me for years. > > > > class DeliveryAddress(Base, Address): > > ... > > delivery_method = Column(String) > > ... > > > > @event.listens_for(DeliveryAddress, 'before_insert') > > def before_insert_deliveryaddress(mapper, connection, target): > > settings = object_session(target).query(Settings).one() > > target.delivery_method = settings.default_delivery_method > > > > Is there absolutely no way to encapsulate this logic cleanly in the > actual class? > > > I use an attribute event for that, and more often just a simple > @validates. > > @validates(“default_delivery_method”) > def _set_delivery_method(self, key, value): > self.settings.delivery_method = value > return value > > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.