Search code examples
pythonmongodbmongoengineflask-mongoengine

mongoengine - RuntimeError: generator raised StopIteration


I have flask-mongoengine application and I have a seriuos problem when I want to iterate over a mongoengine queryset object. here is the code for my mongoengine object:

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}

and I want to iterate over them like this:

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'

now here is the catch: when run my code (with python 3.5.2) using a local mongo server it works fine (whether Candid collection is empty or not), but I deploy the code on a dockerized server (with python 3.7.0) I get the following Runtime Error:

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

btw, mongoengine version is the same for local and docker server run: mongoengine==0.15.0.

please tell me if I have to provide more information, and any help would be greatly appriciated.


Solution

  • The problem is that raising StopIteration inside generators became deprecated in python 3.5, raised a warning in python 3.6 and now raises an error in python 3.7. It sounds like this package is not ready for python 3.7 yet.

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