我想记录一些SQLAlchemy-Models将要完成的操作.
所以,我有一个after_insert,after_delete和before_update钩子,在那里我将保存模型的先前和当前表示,
def keep_logs(cls):
@event.listens_for(cls, 'after_delete')
def after_delete_trigger(mapper, connection, target):
pass
@event.listens_for(cls, 'after_insert')
def after_insert_trigger(mapper, connection, target):
pass
@event.listens_for(cls, 'before_update')
def before_update_trigger(mapper, connection, target):
prev = cls.query.filter_by(id=target.id).one()
# comparing previous and current model
MODELS_TO_LOGGING = (
User,
)
for cls in MODELS_TO_LOGGING:
keep_logs(cls)
Run Code Online (Sandbox Code Playgroud)
但是有一个问题:当我试图在before_update钩子中找到模型时,SQLA返回修改(脏)版本.如何在更新之前获取以前版本的模型?是否有不同的方法来保持模型更改?
谢谢!