Python SQLAlchemy-如何在提交列之前对其进行处理?

Python SQLAlchemy-如何在提交列之前对其进行处理?,python,orm,insert,sqlalchemy,Python,Orm,Insert,Sqlalchemy,我有一个简单的用户类 我如何才能moidyf我的用户类或监听事件,以便在提交之前将密码更改为salt版本?我不希望每次都执行user.name=salted(username),而是希望它以某种方式内置到用户类中 user = User(name='test', password='hashme') Session = sessionmaker(bind=engine) # get a factory mySession = Session() mySession.add(user) mySes

我有一个简单的用户类

我如何才能moidyf我的用户类或监听事件,以便在提交之前将密码更改为salt版本?我不希望每次都执行
user.name=salted(username)
,而是希望它以某种方式内置到用户类中

user = User(name='test', password='hashme')
Session = sessionmaker(bind=engine) # get a factory
mySession = Session()
mySession.add(user)
mySession.commit()
user = User(name='test', password='hashme')
Session = sessionmaker(bind=engine) # get a factory
mySession = Session()
mySession.add(user)
mySession.commit()
Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(Text)
    password = Column(Text) # should be a hash

    @staticmethod
    def _hash_password(mapper, connection, target):
        user = target
        user.password = hash_method(user.password)


listen(User, 'before_insert', User._hash_password)
listen(User, 'before_update', User._hash_password)