'AttributeError: 'Userz' object has no attribute 'active'

I'm building a flask sqlalchemy app and have run into issues with login_user. I have looked at solutions to this issue but none seem to apply.

My models look like this:

from project import db, app, login_manager
from flask_security import UserMixin, RoleMixin

@login_manager.user_loader
def load_user(user_id):
    return Userz.query.get(int(user_id))

class RolesUsers(db.Model):
    __tablename__ = 'roles_users'
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column('user_id', db.Integer(), db.ForeignKey('userz.id'))
    role_id = db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))

class Role(db.Model, RoleMixin):
    __tablename__ = 'role'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class Userz(db.Model, UserMixin):
    __tablename__ = 'userz'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    email_conf = db.Column(db.Boolean(), default=False)
    email_conf_date = db.Column(db.DateTime)
    password = db.Column(db.String(60), nullable=False)
    roles = db.relationship('Role', secondary='roles_users',
                         backref=db.backref('users', lazy='dynamic'))

My login route:

from flask_login import login_user, current_user, logout_user, login_required

@app.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = Userz.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)   
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('home'))     
        else:    
            flash('Login Unsuccessful. If you have already confirmed your email, please check email and password', 'danger')

    return render_template('login.html', title='Login', form=form)

I've never had this issue before as I've used a similar patter in the past. I read through other questions on the topic and they all pointed to not having UserMixin inherited in my User model but as you can see I do.

When I debug it takes issue with the line login_user(user, remember=form.remember.data)

And raises AttributeError: 'Userz' object has no attribute 'active'

The form is validating so I'm really at a loss here. Any help would be much appreciated!

Edit: in the debugger I noticed that "current_user" is "None" even though I just logged the user in. Where is the disconnect?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source