简体   繁体   English

使用sqlalchemy设置值后如何运行“设置”事件侦听器

[英]How to run a 'set' event-listener after the value has been set with sqlalchemy

I have an event-listener in an sqlalchemy-project, which i want to run after the target and one other value have been saved. 我在sqlalchemy项目中有一个事件侦听器,我想在保存目标和另一个值之后运行它。

@event.listens_for(Component.name, 'set')
def do_things(target, value, oldvalue, initiator):
    print(target.someOtherValue)

now target.someOtherValue only sometimes holds the new value(indicating a timing issue?). 现在target.someOtherValue有时仅保存新值(指示计时问题?)。

How can I make sure to always run the function after the new values are saved? 保存新值后,如何确保始终运行该函数?

Had a similar problem but resolved it with the 'before_update' event. 发生了类似的问题,但通过“ before_update”事件解决了该问题。 Hope it works for you too. 希望它也对您有用。

@event.listens_for(SomeClass, 'before_update')
def receive_before_update(mapper, connection, target):
"listen for the 'before_update' event"

# ... (event handling logic) ...

NB: Make sure not to add .name on your SomeClass object. 注意:请确保notSomeClass对象上添加.name Use the target.yourclassproperty to refer to properties specific to a particular class. 使用target.yourclassproperty引用特定于特定类的属性。

Read more about Sqlalchemy's Mapper Events 阅读有关Sqlalchemy的Mapper事件的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM