【问题标题】:Tracking changes in SQLAlchemy model and getting json_body user_id跟踪 SQLAlchemy 模型的变化并获取 json_body user_id
【发布时间】:2016-03-18 00:12:24
【问题描述】:

我正在使用Tracking model changes in SQLAlchemy 的帖子中的如下代码。我目前遇到的唯一问题是我的地图通过令牌获取最终用户的用户名,我需要将该用户名信息传递到此函数中,以便记录更改。有没有更好的方法,还是我只需要在我的表中添加一个名为 last modified by 的列,然后在更新表时用这个监听器刮掉它?

@event.listens_for(cls, 'before_update')
def before_udpate(mapper, connection, target):
    state = db.inspect(target)
    changes = {}

    for attr in state.attrs:
        hist = state.get_history(attr.key, True)

        if not hist.has_changes():
            continue

        # hist.deleted holds old value
        # hist.added holds new value
        changes[attr.key] = hist.added

        # now changes map keys to new values

或者有什么方法可以让我从我的视图中调用这些信息?

【问题讨论】:

    标签: python sqlalchemy pyramid


    【解决方案1】:

    睡了一晚之后。我想出了一个混合。类似于此页面上的内容:http://francesco.pischedda.info/posts/logging-model-changes-with-sqlalchemy-listeners.html

    我传入我的请求对象,以便我的目标对象将拥有请求对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多