如何使用“用户"列进行交易? [英] How to use the User column for transactions?

查看:81
本文介绍了如何使用“用户"列进行交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建议使用sqlalchemy-continuum,它似乎可以很好地满足我的要求.

I got a recommendation to use sqlalchemy-continuum, which seems to do very much what I want out of the box.

默认情况下,ORM假定存在用户列.我确实确实想保存编辑的发起者,但是文档仅说这是您指定用户类的方式",而没有说任何有关在实际使用版本表时如何指定此类对象的信息.

By default, the ORM assumes that there is a user column. I do indeed want to save originators of an edit, but the documentation only says "This is how you specify a User class", without saying anything about how to specify such an object when actually using versioned tables.

如何指定负责交易的用户? 是否有类似的方法可将提交消息/原因/引用(VARCHAR)添加到每个事务中?

How do I specify the User responsible for a transaction? Is there a similar way to add a commit message/reason/citation (a VARCHAR) to each transaction?

推荐答案

如果可以从某个全局范围(例如flask.g或flask.request)中获取用户,则可以创建一个插件,实现transaction_args()方法:

If you can get the user from some global scope (like flask.g or flask.request), you can create a plugin, implementing the transaction_args() method:

from sqlalchemy_continuum.plugins import Plugin


class UserPlugin(Plugin):

    def transaction_args(self, uow, session):
        return {
            'user_id': get_user_from_globals()
        }

请参见 plugins/flask.py pyramid_plugin.py

See plugins/flask.py or pyramid_plugin.py. You should return primary key value of your User model.

您可以使用 TransactionMeta保存其他数据插件放在一个单独的表中.没有记录的API可以扩展Transaction类本身.它由TransactionFactory创建,但是在以后的版本中可能会更改.分开保存其他数据可能是个好主意.

You can save additional data with TransactionMeta plugin in a separate table. There is no documented API for extending the Transaction class itself. It is created by TransactionFactory, but that could change in later versions. It is probably a good idea to keep additional data separate.

这篇关于如何使用“用户"列进行交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆