How can I connect MongoDB to my Django Project?


MongoDB is Non-SQL database, where you can store all kinds of information without sticking to strict rules — you just toss in what you need, and it works. It’s perfect for apps where your data keeps changing or doesn’t fit neatly into rows and columns. Django, on the other hand, is web framework for Python. It handles the boring, complex parts like user login, page routing, and talking to databases, so you can focus on building your app. Normally, Django works with SQL databases, but with some extra tools, it can work with MongoDB too.
There are many ways to connect both of them.
Djongo
Pymongo
django_mongo_backend [MOST EFFECTIVE AND HIGHLY RECOMMENDED]
Djongo
Djongo is an improvement over PyMongo in that developers need not write lengthy queries. It maps Python objects to MongoDB documents, i.e., Object Document Mapping (ODM). Djongo ensures that only clean data enters the database. By performing integrity checks, applying validations, etc. with Djongo, there is no need to modify the existing Django ORM.
Install Djongo:
pip install djongo
Now, go to your project folder (example, MyFirstDjangoProj), and open settings.py file. You can edit it on Textpad, Python IDE, or any editor. Search for DATABASES, and change the settings to point to MongoDB. The ENGINE will be djongo and the database name (NAME) will be your MongoDB database name.
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'db-name',
}
}
If your database is not on localhost or is secured, you should also fill in the CLIENT information like HOST, USERNAME, PASSWORD, etc.
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-db-name',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host': 'mongodb+srv://<username>:<password>@<atlas cluster>/<myFirstDatabase>?retryWrites=true&w=majority'
}
}
}
Pymongo
This is the official MongoDB driver for Python. It gives you full control over MongoDB queries, but you won’t be using Django’s ORM. Instead, you write your own queries directly in Python code — great if you want more flexibility and don’t mind writing a bit more code yourself.
If you want to try it you can visit the official documentation here
Django MongoDB Backend [Most Effective and Highly Recommended]
A common issue with third-party libraries is that they are often outdated or not actively maintained. To address this, MongoDB has released its official backend for Django, ensuring easier integration and fewer errors.
Install the official MongoDB Django backend
pip install django_mongo_backend
Then Go to the destination folder of your projects. For me its “E://Projeccts”. Open up cmd and paste bellow link to create the project
django-admin startproject <projectname> --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.1.x.zip
Replace <projectname> with your Project Name. Here its using mongodb’s template, only difference between usual one and this is, it is specifically configured for the MongoDB connection.
After running the command,your project has the following file structure:
projectname/
manage.py
mongo_migrations /
__init__.py
contenttypes/
auth/
admin/
projectname/
__init__.py
apps.py
settings.py
urls.py
asgi.py
wsgi.py
Update your database setting
Open your settings.py
file and navigate to the DATABASES
setting. Replace this setting with the following code:
DATABASES = { "default": django_mongodb_backend.parse_uri("<connection string URI>", db_name="<database name>"),}
Replace the <connection string URI>
placeholder with the connection string that you copied from the Create a Connection String step of this guide. This configures your Django app to connect to your Atlas deployment and you can run the server.
Thanks to MongoDb and Django Team.
Subscribe to my newsletter
Read articles from Aaron Shenny directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
