Connecting Django with MongoDB: All the Versioning Shenanigans Dissected (2022 edition)
shlokabhalgat
Posted on March 24, 2022
Trust me, I have tried connecting Django with MongoDB using pymongo, mongoengine, and djongo finally. Although MongoDB’s website gives in-depth and stepwise instructions to do it, there are some versioning issues that need to be sorted out before aiming for that “Huge” (Mongo literally translates to huge 😛) Database to do wonders for your project.
Here I am assuming you are using the latest Django version 2.2.26 and Python version 3.6+
Skip Step 1 if you already have MongoDB installed. Please check the version once to have a better understanding of the 2 drivers that we will use to connect with Django.
To check MongoDB version use-
mongod --version
Step 1
Install MongoDB on local from terminal using the following command-
brew install mongodb-community@5.0
For Windows, you need to get the installer and follow the steps as given here
Step 2
Download MongoDB Compass from here. It gives a user-friendly interface like SQLWorkbench and you can even run a MongoDB cluster for free from there. The application looks something like this-
Step 3
Connecting to a Django Project
- Using Djongo
Install djongo through terminal-
pip install djongo
Coming to actually connecting MongoDB with your Django project using djongo, all we need to do is add the following to the settings.py file-
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'YOUR_DATABASE_NAME',
'HOST': 'localhost',
'PORT': '27017'
}
}
-
Using MongoEngine
To connect MongoDB with your Django project using mongoengine, you need to add the following to the settings.py file-
_MONGODB_USER = ""
_MONGODB_PASSWD = ""
_MONGODB_HOST = "localhost"
_MONGODB_NAME = "YOUR_DATABASE_NAME"
_MONGODB_PORT = 27017
_MONGODB_DATABASE_HOST = "mongodb://%s:%s@%s/%s" % (
_MONGODB_USER,
_MONGODB_PASSWD,
_MONGODB_HOST,
_MONGODB_NAME,
)
mongoengine.connect(_MONGODB_NAME, host=_MONGODB_HOST, port=_MONGODB_PORT)
After that you can import it in your models.py file and use it-
-
Using PyMongo
The latest pymongo version is 4.0.2 which is incompatible with the latest mongoDB v5.0. I got this error after which I downgraded the pymongo version.
pymongo.errors.ConfigurationError: Server at xxxx:xx reports wire version 5, but this version of PyMongo requires at least 6 (MongoDB 3.6).
pymongo v 3.12.1
MongoDb v 5.0.5
Source - pypi.org
Because of versioning shenanigans I was turned off.
Key Takeaways
- I would highly recommend using mongoengine for its ease to access collections through models.
-
Djongo is very easy to connect with Django. ( I had tried to connect all three and somehow pymongo’s version incompatibility wouldn’t allow me to connect with djongo)
Apparently, Djongo has dependencies on pymongo and sqlparse so we need to get the version right for pymongo after installing Djongo-
Now if you go up and check pymongo’s versioning issues, you will know that MongoDB v5.0 is not compatible with PyMongo’s v 4.0
To downgrade pymongo’s version run this command-
python3 -m pip install pymongo==3.12.1
- Hence using the following combinations did wonders for me -
- Djongo v 1.3.6
- Pymongo v 3.12.1
- MongoDB v 5.0.5
Posted on March 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
March 24, 2022