mongoengine - RuntimeError: generator raised StopIteration

mongoengine - RuntimeError: generator raised StopIteration

我有 flask-mongoengine 应用程序,当我想遍历 mongoengine 查询集对象时遇到严重问题。这是我的 mongo 引擎对象的代码:

mongo_models:

class Candid(Document):
   candid_intent_id = StringField()
   id_list = ListField(StringField())
   custom_code = StringField()
   is_approved = BooleanField()

   def to_json(self, *args, **kwargs):
       return {'candid_intent_id': self.candid_intent_id,
               'id_list': self.id_list,
               'custom_code': self.custom_code,
               'is_approved': self.is_approved}

我想像这样遍历它们:

candid_list:

custom_code = 'Bob'
query_set = Candid.objects(is_denied=False, custom_code=current_request.custom_code)
try:
    for candid in query_set:
        suggested_intent_list.append(candid.to_json())
        if not candid.is_approved:
            suggested_intent_count += 1
except StopIteration:
    return 'StopIteration error'

现在问题来了:当 运行 我的代码(使用 python 3.5.2)使用本地 mongo 服务器时,它工作正常(无论 Candid 集合是否为空),但我将代码部署在 docker 化的服务器上(使用 python 3.7.0)我收到以下运行时错误:

File "/controller.py", line 25, in <candid_list>
   for candid in query_set:
RuntimeError: generator raised StopIteration

顺便说一句,mongo本地和 docker 服务器的引擎版本相同 运行:mongoengine==0.15.0.

如果我需要提供更多信息,请告诉我,如有任何帮助,我们将不胜感激。

问题是在 python 3.5 中不推荐在生成器中引发 StopIteration,在 python 3.6 中引发警告,现在在 python 3.7 中引发错误。听起来这个包还没有为 python 3.7 做好准备。

https://www.python.org/dev/peps/pep-0479/#transition-plan

正如@FHTMitchell 指出的那样,问题出在我本地机器 (3.5.2) 和 docker 服务器 (3.7.0) 上的 python 版本之间的差异。我将 docker 图像的 python 更改为 python:3.5-slim,它解决了问题。希望它能帮助别人。 但最重要的是,我从中得到的教训是: 确保您的开发和部署环境匹配!

升级 mongoengine 解决了我的问题。需要更新旧版本(或导致此问题的版本)已修复此错误。

pip install mongoengine --upgrade