0

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
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
aeRogelio
  • 37
  • 1
  • 10

1 Answers1

0

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
amol
  • 1,771
  • 1
  • 11
  • 15
  • Thank you very much for answering, but i can bot be putting "transaction.doom()" everywhere, do this there will be some default settings – aeRogelio Mar 05 '14 at 17:33
  • If you need to disable it for the whole project just pust `base_config.use_transaction_manager = False` in your `config/app_cfg.py` file – amol Mar 05 '14 at 18:20