My app fails whenever I call login_user with the error NotImplementedError: No 'id' attribute - override 'get_id'. My user has an id attribute. Why does this fail?
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.verify_password(form.password.data):
print(user.user_id)
login_user(user, False)
return jsonify({'response': user.user_id})
class User(UserMixin, db.Model):
__tablename__ = 'users'
user_id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(64), unique=True, index=True)
email = db.Column(db.String(64), unique=True, index=True)
password_hash = db.Column(db.String(128))
You just need to add a get_id() function in order to override the default properties of get_id() under the User class in the models.py file where your database schema is defined.
class User(#...):
# ...
def get_id(self):
return (self.user_id)
# ...
login_user calls get_id on the user instance. UserMixin provides a get_id method that returns the id attribute or raises an exception. You did not define an id attribute, you named it (redundantly) user_id. Name your attribute id (preferably), or override get_id to return user_id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With