SQLAlchemy表属性只需要2个参数(给定1个)

时间:2016-08-26 18:31:27

标签: python sqlalchemy

我有以下SQLAlchemy类:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    password = db.Column(db.String(100))

    @property
    def encrypt_password(self, password):
        self.password = hash_password(password)

当我使用该属性时,我收到错误encrypt_password() takes exactly 2 arguments (1 given)

user = db.session.query(User).filter_by(id=id).one()
user.encrypt_password('mypassword')

为什么这不起作用?如何实现设置密码的属性?

1 个答案:

答案 0 :(得分:0)

您将该属性的setter定义为password。删除property,因为这是一个函数,或正确定义属性。

如果您要前往属性路径,请重命名from sqlalchemy.ext.hybrid import hybrid_property _password = db.Column('password', db.String) @hybrid_property def password(self): return self._password @password.setter def password(self, value): self._password = hash_password(value) 属性并通过getter的属性访问它。使用hybrid_property,以便该列仍可直接查询。

user.password = 'stack overflow'  # gets hashed
user.password  # hashed value
session.query(User).filter(User.password.is_(None))  # query for users without passwords
for ($p=0;$i<=2880;$p+10){
$sql = "
select sum(rotations) 
  from wheel 
 where time BETWEEN (DATE_ADD(NOW() , INTERVAL - ".$p." MINUTE) AND (DATE_ADD(NOW() ,INTERVAL - ".$p+10." MINUTE)
"; 
}