Read This Before Using Django With MongoDB

Dennis Ivy
4 min readAug 8, 2022

--

Q: Can I use MongoDB with Django?

A: Yes, BUT… not in the way you think.

Let’s talk about how this works and the things you should know before you try this approach.

This article was also made into a video, see here

A short story.

MongoDB + Python

I’ve recently taken to MongoDB and NoSQL documented oriented databases as a whole after working on some NodeJS projects. As someone with more of a python background I naturally wanted to try using MongoDB with something more common in my day to day life, so I started testing things out by using MongoDB with Flask & Fast API. The integration was actually seamless since MongoDB manages a package called PyMongo, which essentially gives us a way of using python to connect to and interact with a MongoDB Database.

MongoDB also worked well with these frameworks because they are not opinionated about how you connect to your database and already used third party ORM’s like SQL Alchemy.

The issue however comes in when you try to use a more opinionated Framework like Django which is built with relational databases in mind.

Django Databases

Django officially supports the databases listed below and also has a number of third party packages to support similar types of databases.

- PostgreSQL
- MariaDB
- MySQL
- Oracle
- SQLite

It’s ORM queries are built with SQL in mind. Basically, Django’s ORM maps functions to SQL queries and that's how we read, write and migrate our database.

The Djongo Package

Yes that’s Djongo, not Django, with an “O” instead of an “A”.

In doing some research I stumbled upon a package called “Djongo” which essentially tries to map Django's “ORM” to Mongo DB, but there are a few problems with Djongo.

1 —Djongo is an experiment — The idea here is really cool, I love it! But Djongo is incomplete and only maps basic queries without keeping all the functionality in mind. Sure you can make some basic reads and writes but anything outside of that is going to have to be hacked together by you as the developer.

2 — Djongo is not supported — Reading through Djongo’s documentation & it’s github repo I realized this is a ghost town, except the issues section. Last I checked, there were 298 open issues and no end in site for support.

3 — No compatibility beyond Django 3.0.5 — With little support for the package and Django updates, at this point, I can only use Djongo with Django 3.0.5. Any later versions and I can’t get the thing connected. Many others have the same issue.

I recently spoke to a developer advocate from MongoDB who said the following about the Djongo package:

“It seems that Djongo is currently the only way to effectively use MongoDB + Django. Unfortunately, it is very much a hack and the repo seems to be sporadically maintained. We don’t have any contact with its maintainer.

The main issue is that it basically just converts SQL queries to MongoDB queries, which doesn’t really make sense in a document database. You will not get any of the benefits of MongoDB. And it could actually result in poor performance in MongoDB vs SQL.” — Jesse (AKA CodeStackr)

In short, you are comparing two VERY different types of databases. In order to make this work. There needs to be a package with all of Django's core functionality in mind and there certainly needs to be some time put into it. It’s no cake walk.

However, there is some light at the end of the tunnel. In the same conversation with Jesse he said the following:

“Apparently we’ve been looking into Django support for a while now but the built-in ORM is very much tied to SQL which makes support very difficult.

If you have any ideas, we very much want to find a way to get into the Django community!”

How to use Django with MongoDB today with PyMongo

Just because Djongo is not a fit, it doesn't mean all hope is lost. There will however, be some major trade offs.

My suggestion, and this is something I am testing out myself, is to bypass the default built in way of using the Django ORM and connect directly using PyMongo.

This is possible but keep in mind you will lose ALOT of the functionality that Django gives you. So I’m not exactly recommending it, but I’m hoping a few of you crazies will try this with me.

If you go this route, consider the following

  • No more Django ORM — You’ll have to make your queries from scratch so forget everything you know about the default “Django way”.
  • No More Admin Panel — I really don’t see any way you can use the admin panel if your not using Django’s ORM so scratch this off the list.
  • Database Connection — The DATABASES connection object in settings.py does nothing for us anymore, this will have to be done by hand using PyMongo and your own config file.
  • No more migrations — Forget about running makemigrations. with a direct connection using PyMongo, we are no longer running automatic migrations.
  • Authentication & Models — This part I am still unsure about. If we are no longer using the Django ORM, how will authentication work with the user model? This part may have to be re-written from scratch.

There are a lot of things to consider here. Basically, any Django functionality having to do with the database will have to be ignored. Personally I plan to give this a try and am optimistic that there is a path.

MongoDB is a powerful database, so why not find a way to use it with a powerful framework like Django?

--

--

Dennis Ivy

YouTuber, contributor at @traversymedia , software developer at @appwrite and online instructor.