我正在创建表示用户凭证的SQLAlchemy类。SQL Alchemy覆盖==
我想要有存储密码哈希值的字段password
。因此,我想覆盖其行为方式如下:
当分配
credentials.password = value
它实际存储值当比较
credentials.password == value
它实际上与价值的哈希值进行比较的哈希
我已阅读以下SQLAlchemy文档部分http://docs.sqlalchemy.org/en/rel_0_7/orm/mapper_config.html#using-descriptors-and-hybrids
我认为我确实懂得如何解决第1号问题。
但我不确定,第二点如何做。有没有办法做到这一点安全的方式(不破坏SQLAlchemy)?
下面是示例模型:
class Credentials(Base):
__tablename__ = 'credentials'
id = Column(Integer, primary_key=True)
_password = Column('password', String)
@hybrid_property
def password(self):
return self._password
@password.setter(self):
self._password = hash(self._password)
您可以发布您的当前模型和小型测试用例吗? – plaes
通过testcase我的意思是一个小型自包含脚本,就像这里http://stackoverflow.com/questions/9140015/how-to-get-rows-which-match-a-list-of-3-tuples-conditions-与-SQLAlchemy的/ 9140163#9140163 – plaes