{"id":8995,"library":"flask-mongoengine","title":"Flask-MongoEngine","description":"Flask-MongoEngine is a Flask extension that provides seamless integration with MongoEngine for MongoDB object-document mapping and also supports WTF model forms. The current version is 1.0.0. Releases are somewhat infrequent but significant, with major version bumps often including breaking changes related to Python, Flask, MongoEngine, and PyMongo dependency updates.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/mongoengine/flask-mongoengine","tags":["flask","mongoengine","mongodb","orm","odm"],"install":[{"cmd":"pip install flask-mongoengine","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Web framework integration","package":"Flask","optional":false},{"reason":"ODM for MongoDB","package":"MongoEngine","optional":false},{"reason":"WTF form integration","package":"Flask-WTF","optional":false},{"reason":"MongoDB Python driver","package":"pymongo","optional":false}],"imports":[{"symbol":"MongoEngine","correct":"from flask_mongoengine import MongoEngine"},{"note":"MongoEngine's Document class is used directly, not through flask_mongoengine","symbol":"Document","correct":"from mongoengine import Document, StringField, IntField"}],"quickstart":{"code":"import os\nfrom flask import Flask\nfrom flask_mongoengine import MongoEngine\nfrom mongoengine import Document, StringField, IntField\n\napp = Flask(__name__)\n\n# Configure MongoDB connection\n# Use an environment variable for production readiness\napp.config[\"MONGODB_HOST\"] = os.environ.get(\"MONGODB_URI\", \"mongodb://localhost/testdb\")\n\ndb = MongoEngine(app)\n\nclass User(Document):\n    name = StringField(required=True)\n    age = IntField()\n    meta = {'collection': 'users'}\n\n@app.route('/')\ndef index():\n    # Ensure collection is empty for repeatable test runs\n    User.drop_collection()\n\n    # Create a user\n    user = User(name=\"Alice\", age=30)\n    user.save()\n\n    # Create another user\n    User(name=\"Bob\", age=25).save()\n\n    # Find users\n    all_users = User.objects.all()\n    names = [u.name for u in all_users]\n\n    return f\"<h1>Users in MongoDB:</h1><p>{', '.join(names)}</p>\"","lang":"python","description":"This quickstart sets up a basic Flask application with Flask-MongoEngine, connects to a local MongoDB instance (or one specified by MONGODB_URI), defines a simple 'User' document, and demonstrates creating and retrieving users via a Flask route."},"warnings":[{"fix":"Upgrade your Python environment to 3.6 or newer. If upgrading Python is not an option, you must use Flask-MongoEngine <1.0.0 (e.g., `pip install \"flask-mongoengine<1.0.0\"`).","message":"Flask-MongoEngine v1.0.0 dropped support for Python versions 2.7 through 3.5. Applications using these Python versions will fail to install or run.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure PyMongo is updated to version 3.6.0 or newer: `pip install --upgrade pymongo`.","message":"Flask-MongoEngine v1.0.0 dropped support for PyMongo versions older than 3.6.0. An incompatible PyMongo version will lead to configuration errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure MongoEngine is updated to a compatible version (0.10+ for v0.8, and 0.15+ for v1.0.0): `pip install --upgrade mongoengine`.","message":"Flask-MongoEngine v0.8 dropped support for MongoEngine 0.7. Using an older MongoEngine version will lead to various compatibility issues and errors.","severity":"breaking","affected_versions":">=0.8"},{"fix":"Update MongoEngine and Flask-MongoEngine to their latest versions. Replace `help_text` with `description` in `StringField`, `IntField` etc. if directly passing them to `mongoengine.fields`.","message":"MongoEngine's `help_text` and `safe` attributes for fields were deprecated prior to Flask-MongoEngine v0.7.5. While Flask-MongoEngine handles some of these changes, direct usage in custom forms might still lead to warnings or errors in newer MongoEngine versions.","severity":"deprecated","affected_versions":"<0.7.5 (and older MongoEngine versions)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Upgrade PyMongo to a compatible version: `pip install --upgrade pymongo`.","cause":"You are using Flask-MongoEngine v1.0.0 or newer, but your PyMongo installation is older than version 3.6.0, which is no longer supported.","error":"pymongo.errors.ConfigurationError: The 'PyMongo' package requires PyMongo >=3.6.0."},{"fix":"Upgrade your Python environment to 3.6 or newer. If upgrading Python is not feasible, downgrade Flask-MongoEngine to a compatible version (e.g., `pip install \"flask-mongoengine<1.0.0\"`).","cause":"This error, particularly on `f\"...\"` strings or `async`/`await` keywords, indicates you're running Flask-MongoEngine v1.0.0+ on an unsupported Python version (e.g., Python 2.x or <3.6).","error":"SyntaxError: invalid syntax"},{"fix":"Ensure `db = MongoEngine(app)` is called after defining `app` and its configuration. Verify your `mongoengine` package version is compatible with your `flask-mongoengine` version (e.g., `mongoengine>=0.15.0` for `flask-mongoengine>=1.0.0`).","cause":"This typically means the MongoEngine instance was not correctly initialized with the Flask app, or the Document class was not defined before `db = MongoEngine(app)` was called, or an incompatible MongoEngine version is installed.","error":"mongoengine.errors.NotRegistered: Document type 'MyDocument' not registered."},{"fix":"Ensure your MongoDB server is running and accessible. Double-check the `MONGODB_HOST` configuration string in your Flask app to ensure it points to the correct address and port (e.g., `mongodb://localhost:27017/mydb`).","cause":"The Flask application could not establish a connection to the MongoDB server. This usually means the server is not running or the `MONGODB_HOST` configuration is incorrect.","error":"pymongo.errors.ConnectionFailure: [Errno 111] Connection refused"}]}