I am creating SQLAlchemy class that represents user credentials.
I want to have field password
that stores hashed value of password. Therefore I would like to override its behavior the following way:
When assigned
credentials.password = value
it actually stores hash of the valueWhen comparing
credentials.password == value
it actually compares with hash of the value
I have read the following part of SQLAlchemy documentation http://docs.sqlalchemy.org/en/rel_0_7/orm/mapper_config.html#using-descriptors-and-hybrids
And I think I do understand how to solve the issue number 1.
I am however unsure, how to do second point. Is there a way to do it the safe way (without breaking SQLAlchemy)?
Here is the example model:
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)