I have been going back and forth over which Python API to use when interacting with Mongo. I did a quick survey of the landscape and identified three leading candidates.
If you were designing a new content-heavy website using the django framework, what API would you choose and why?
MongoEngine looks like it was built specifically with Django in mind. PyMongo appears to be a thin wrapper around Mongo. It has a lot of power, though loses a lot of the abstractions gained through using django as a framework. Ming represents an interesting middle ground between PyMongo and MongoEngine, though I haven't had the opportunity to take it for a test drive.
Python needs a MongoDB driver to access the MongoDB database. In this tutorial we will use the MongoDB driver "PyMongo". We recommend that you use PIP to install "PyMongo". PIP is most likely already installed in your Python environment.
The three most widely used Database Management Systems for Django are SQLite, MySQL, and PostgreSQL. The Django community and official Django documentation state PostgreSQL as the preferred database for Django Web Apps.
PyMongo is the official distribution recommended by Python to connect your Django application to a MongoDB database. PyMongo is an open-source Python distribution containing tools for interacting with MongoDB. It makes it easier to perform CRUD operations (Create, Read, Update, and Delete) with a MongoDB database.
As Mike says, you can't avoid PyMongo - all the other interfaces build on top of it. These other interfaces are arguably unnecessary. ORMs such as that used in Django are useful when dealing with SQL because they mitigate the complexity of creating SQL queries and schemas, and parsing result sets into objects.
PyMongo however already has that covered - queries go through a convenient and simple API and results coming from MongoDB already are objects (well, dicts in Python - same difference) by definition. If you feel that you really need to decorate your Mongo documents with Python objects, it's easy to add a SON manipulator to PyMongo. The nice thing about this approach is that you can write code directly on PyMongo, and slide in additional functionality later on without having to insert a new API between your code and PyMongo.
What's left? Schema creation and migration are somewhat useful, but are almost as simply done ad-hoc - chances are if you're considering using MongoDB you want to break out of the traditional SQL-style model anyway. Also, if there were a fully Django-compatible MongoDB ORM you might get some mileage out of it. Anything less than that and you will probably be creating work for yourself.
You won't regret using PyMongo directly.
One last option worth watching if you are interested in top efficiency is the asynchronous version of PyMongo, here: http://github.com/fiorix/mongo-async-python-driver
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With