[FIXED] AttributeError: 'BaseQuerySet' object has no attribute 'is_authenticated'

Issue

I'm using flask-login and MongoDB as my database to store user profiles.

When checking whether a user is authenticated in my login function:

@bp.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('routes.index')) 

I'm getting the following error:

AttributeError: 'BaseQuerySet' object has no attribute 'is_authenticated'

My User object is extending UserMixin from flask-login.

Any idea what might me wrong?


Solution

OK i found it - in my user_loader function i did something like that:

@login.user_loader
def load_user(id):
    return User.objects(_id=ObjectId(id))

whereas the correct way to get a single result (not entire collection) using mongoengine (which apparently translates _id into id), would be this:

@login.user_loader
def load_user(id):
    return User.objects(id=ObjectId(id)).first()


Answered By - Mariusz
Answer Checked By - Terry (Python.Easybugfix Volunteer)

Related Posts

Post a Comment