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.

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:

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:

base_config.use_transaction_manager = False
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top