0

I am developing a Flask/MongoDB application Im deploying on Azure. Locally, I am in the process of creating my models and testing my database connection. I am using Flask-MongoEngine to manage my DB connection. This is a sample of code that works perfectly on localhost but fails when calling its deployed version on Azure.

# On models.py

from flask_mongoengine import MongoClient
db = MongoClient()

class User(db.Document):
    name = db.StringField(max_length=50)
    token  = db.StringField(max_length=50)
    email  = db.EmailField()

Later, from views.py I call my User class like this:

import models as mdl

@app.route('/test')
def test():
    """For testing purposes"""
    user = mdl.User(name='Matias')
    user.save()
    users = mdl.User.objects
    return jsonify(users)

which ouputs as expected locally. On Azure, however, I get the following error (will only show the last and relevant part of the traceback):

File ".\app\views.py", line 53, in test
    user = mdl.User(name='Matias')
  File "D:\home\python364x86\lib\site-packages\mongoengine\base\document.py", 
line 43, in _init_
    self._initialised = False
  File "D:\home\python364x86\lib\site-packages\mongoengine\base\document.py", 
line 168, in _setattr_
    self._is_document and
AttributeError: 'User' object has no attribute '_is_document'

Through pip freeze I checked I am using the same versions for mongoengine, pymongo and flask_mongoengine in both environments. I can't seem to find someone else with the same problem. The app is deployed as a webapp on a Windows machine in the Azure cloud.

Any help is appreciated, thanks.

PS: Further info
Reviewing mongoengine code, I found out that _is_document attribute is set inside a metaclass for the class Document (DocumentMetaclass and TopLevelDocumentMetaclass). I tried setting the attribute to True inside User, and the following error showed:
AttributeError: 'User' object has no attribute '_meta',
which is also an attribute defined inside those metaclasses. Somehow the metaclasses code is not running? Maybe the Azure environment has something to do with it?

  • Your trace back doesn’t match your code. – Laurent LAPORTE Jul 11 '18 at 04:15
  • Check that you've got the same versions of Mongo and the flask_mongoengine library installed on Azure as you do locally – Tim Thompson Jul 11 '18 at 08:08
  • @TimThompson I have. I also got rid of `flask-mongoengine` and implemented the same solution using pure `mongoengine`, got the same error. Have `pymongo 3.4.0` and `mongoengine 0.15.0` in both environments. I am at wits end. – Matias Correa Jul 11 '18 at 14:46

0 Answers0