提交某种类型的模型后运行功能

问题描述 投票:7回答:1

我想在提交Post模型的实例时运行一个函数。我想在它们提交后的任何时间运行它,所以我不想在任何地方显式调用该函数。我该怎么办?def notify_subscribers(post): """ send email to subscribers """ ... post = Post("Hello World", "This is my first blog entry.") session.commit() # How to run notify_subscribers with post as argument # as soon as post is committed successfully? post.title = "Hello World!!1" session.commit() # Run notify_subscribers once again.

我想在提交Post模型的实例时运行一个函数。我想在它们提交时随时运行它,所以我不想在任何地方显式调用该函数。我怎样才能做到这一点? ...
python flask sqlalchemy flask-sqlalchemy
1个回答
3
投票
无论下面您选择哪个选项,SQLAlchemy都附带a big warning about the after_commit event(这是两种方式都发送信号的时间。)>]

当调用after_commit事件时,会话不在活动事务中,因此

无法发出SQL

© www.soinside.com 2019 - 2024. All rights reserved.