mongoengine - 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。
知道我可能错了吗?
解决方案
好的,我找到了 - 在我的 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()
推荐阅读
- java - SpotBugs“没有为属性‘spotbugsClasspath’指定值”
- actions-on-google - Google 在每次访问后生成新的 UserId(Alpha 阶段)
- python - 使用 tkinter 和 openpyxl 遍历电子表格的最佳方式?
- php - 处理来自 3rd 方 API 的错误
- java - 能够使用杰克逊循环从 json 响应返回的每个用户
- python - Python 不同大小的元素
- php - 将 php 错误日志写入包含自定义数据的文件
- python - 在 Python 中比较列并显示不相等的行?
- ruby-on-rails - 在 Rails 中如何处理基于用户的动态颜色变化?
- docker - 在 macOS 10.13 上使用 Docker CE 从 ubuntu 基础映像构建 docker 映像时出现连接失败错误