首页 > 解决方案 > AttributeError:“BaseQuerySet”对象没有属性“is_authenticated”

问题描述

我使用烧瓶登录和 MongoDB 作为我的数据库来存储用户配置文件。

在我的登录功能中检查用户是否经过身份验证时:

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

我收到以下错误:

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

我的用户对象是从烧瓶登录扩展 UserMixin。

知道我可能错了吗?

标签: mongoengineflask-loginflask-mongoengine

解决方案


好的,我找到了 - 在我的 user_loader 函数中,我做了类似的事情:

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

而使用 mongoengine(显然将 _id 转换为 id)获得单个结果(不是整个集合)的正确方法是:

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

推荐阅读