简体   繁体   English

sqlalchemy对象已保存以完成操作

[英]sqlalchemy object saved to complete action

Im new to sqlalchemy, Im query object and expunge of session for prevent the session save change, after add the object with change at session but not flush or commit. 我是sqlalchemy的新手,我添加了对象并在会话中进行了更改,但没有刷新或提交,因此我查询对象和会话的删除以防止会话保存更改。

when the ends controller, the session save my object, i dont want that, I want the object is lost if I did not flush or commit 当结束控制器时,会话保存我的对象,我不想要,如果我不刷新或提交,我希望对象丢失

my code: 我的代码:

object = model.DBSession.query(model.Object).filter_by( field = value ).first()

model.DBSession.expunge(object)

object.field = gfhggghfg
object.field2 = hsjsjsjsjs

model.DBSession.add(object)


#finish controller turbogearsr the session save the change. I have autocommit and autoflush = False

You don't even need to expunge the object, as TurboGears provides a transaction manager you can just doom the transaction so that TurboGears will throw it away instead of committing the changes: 您甚至不需要删除对象,因为TurboGears提供了一个事务管理器,您可以毁灭该事务,以便TurboGears将其丢弃而不是提交更改:

import transaction

@expose('mytemplate')
def my_controller(self):
    object = model.DBSession.query(model.Object).filter_by( field = value ).first()
    object.field = 'Hello'

    transaction.doom()  # This will prevent changes to be committed.
    return dict(value=object.field)

To disable it for the whole project edit config/app_cfg.py and add: 要在整个项目中禁用它,请编辑config/app_cfg.py并添加:

base_config.use_transaction_manager = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM