SQLAlchemy table property takes exactly 2 arguments (1 given)
<p>I have the following SQLAlchemy class:</p>

<pre><code>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)
</code></pre>

<p>When I use the property I get the error <code>encrypt_password() takes exactly 2 arguments (1 given)</code>.</p>

<pre><code>user = db.session.query(User).filter_by(id=id).one()
user.encrypt_password('mypassword')
</code></pre>

<p>Why doesn't this work?  How do I implement a property for setting the password?</p>

<p>You defined the <code>getter</code> for the property as the <code>setter</code>.  Either remove the <a href="https://docs.python.org/3/library/functions.html#property" rel="nofollow"><code>property</code></a> because this is a function, or define the property correctly.</p>

<p>If you're going the property route, rename the <code>password</code> attribute and access it through the property for the getter.  Use a <a href="http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html" rel="nofollow"><code>hybrid_property</code></a> so that the column is still queryable directly.</p>

<pre><code>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)
</code></pre>



<pre><code>user.password = 'stack overflow'  # gets hashed
user.password  # hashed value
session.query(User).filter(User.password.is_(None))  # query for users without passwords
</code></pre>

python sqlalchemy