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?