How to get the original value of changed fields?

前端 未结 1 660
夕颜
夕颜 2020-12-10 05:00

I\'m using sqlalchemy as my orm, and use declarative as Base.

Base = declarative_base()
class User(Base):
    __tablename__ = \'users\'

    id          


        
相关标签:
1条回答
  • 2020-12-10 05:42

    \To see if user has been modified you can check if user in session.dirty. If it is and you want to undo it, you can execute

    session.rollback()
    

    but be advised that this will rollback everything for the session to the last session.commit().

    If you want to get the original values and memory serves me correctly, it's the second element of the tuple returned by the sqlalchemy.orm.attributes.get_history utility function. You would have to do

    old_value = sqlalchemy.orm.attributes.get_history(user, 'attribute')[2]
    

    for each attribute that you want.

    0 讨论(0)
提交回复
热议问题