On Jun 2, 2014, at 1:47 AM, Alex Grönholm <[email protected]> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to