”;
MongoEngine integrates beautifully with the following libraries −
marshmallow_mongoengine
marshmallow is an ORM/ODM/framework independent serialization/deserialization library for converting complex datatypes, such as objects, to and from native Python datatypes. Using this extension of MongoEngine, we can easily perform serialize/deserialize operations.
First, create a Document class as usual as follows −
import mongoengine as me class Book(me.Document): title = me.StringField()
Then generate marshmallow schema with the code below −
from marshmallow_mongoengine import ModelSchema class BookSchema(ModelSchema): class Meta: model = Book b_s = BookSchema()
Save a document using the code:
book = Book(title=''MongoEngine Book'').save()
And perform serialization/deserialization using dump(0 and load() using the code below −
data = b_s.dump(book).data b_s.load(data).data
Flask-MongoEngine
This is a Flask extension that provides integration with MongoEngine. Connection management of MongoDB database for your app is handled easily by this library. You can also use WTForms as model forms for your models.
After installation of flask-mongoengine package, initialize flask app with the following settings −
from flask import Flask from flask_mongoengine import MongoEngine app = Flask(__name__) app.config[''MONGODB_SETTINGS''] = { ''db'': ''mydata'', ''host'': ''localhost'', ''port'':27017 } db = MongoEngine(app)
Then define a Document sub class using the below code −
class book(me.Document): name=me.StringField(required=True)
Declare an object of above class and call save() method when a particular route is visited.
@app.route(''/'') def index(): b1=book(name=''Introduction to MongoEngine'') b1.save() return ''success''
extras-mongoengine
This extension contains additional Field Types and any other wizardry.
Eve-MongoEngine
Eve is an open source Python REST API framework designed for human beings. It allows to effortlessly build and deploy highly customizable, fully featured RESTful Web Services.
Eve is powered by Flask and Cerberus and it offers native support for MongoDB data stores. Eve-MongoEngine provides MongoEngine integration with Eve.
Install and import the extension using the code below −
import mongoengine from eve import Eve from eve_mongoengine import EveMongoengine
Configure the settings and initialize the Eve instance.
my_settings = { ''MONGO_HOST'': ''localhost'', ''MONGO_PORT'': 27017, ''MONGO_DBNAME'': ''eve_db'' app = Eve(settings=my_settings) # init extension ext = EveMongoengine(app)
Define a Document class as shown below −
class Person(mongoengine.Document): name = mongoengine.StringField() age = mongoengine.IntField()
Add the model and run the application, finally using the below code −
ext.add_model(Person) app.run()
Django-MongoEngine
This extension aims to integrate MongoEngine with Django API, a very popular Python web development framework. This project is still under development.
”;