문제

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