flask - 如何使用 flask_sqlalchemy 信号将操作记录到数据库?
问题描述
我正在尝试使用 sqlalchemy 信号将数据库操作记录到 actions_log 表,但我收到了这个错误
sqlalchemy.exc.InvalidRequestError:此会话处于“已提交”状态;在此事务中不能发出更多 SQL。
#acionslog model
class ActionsLog(db.Model):
id = db.Column(db.Integer, primary_key=True)
object = db.Column(db.String(50))
action = db.Column(db.String(10), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
#__init__.py
@models_committed.connect
def log_action(sender, changes):
if current_user.is_authenticated:
print(sender)
print(changes)
instance = changes[0][0]
action = changes[0][1]
from app.backend.user.models import ActionsLog
log = ActionsLog()
log.object = str(instance)
log.action = action
log.user_id = current_user.id
db.session.add(log)
db.session.commit()
解决方案
推荐阅读
- python - 具有多个参数的 __str__ 函数的继承错误
- .htaccess - 为什么使用我的子域 URL 会显示不安全的 HTTPS 而不是我的正常域 URL?
- javascript - 无法解构“(中间值)”的属性“数据”,因为它未定义
- css - 将 flex 对齐到行的底部
- sas - 计数器变量不打算重置
- python - 使用 MDAnalysis 获得径向分布函数
- javascript - Vue 和 Quasar - 模块 '"../../node_modules/vue/types"' 没有导出的成员 'defineComponent'
- matlab - 通过单击点图中的某些点打开子图(MATLAB)
- java - Android View layoutInflater 通过字符串获取布局
- excel - VBA或公式在Excel中分隔街道名称和后缀?