I know many developers who use Django, of course with Relational Databases: Postgres, SQL, SQLite.
Getting the news from MongoDB that the Django MongoDB Backend is now in Public Preview! was crazy!!! and Amazing of course.
So I decided to give it a try. I made some rookie mistakes and assumptions(😂😂)
Initially I thought it would be a simple plug and play configuration, so I started out by testing it in already existing Django project, in this case, my starter template.
ChrisDevCode-Technologies
/
django-starter
Django Starter Template
Creating a MongoDB Account & a Cluster
I have used MongoDB on most javascript-based apps, so I know my around it.
You can learn more about creating a cluster here.
A free cluster should work for the testing:
After that, you will need to create users to access the database, once that is done, just give it a few minutes for the cluster to build up.
Once that is done, get the connection string, that looks like this:
mongodb+srv://chrisdevcode:<db_password>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo
So I cloned the repo, installed the initial dependencies and add install the django-mongodb-backend
package:
pip install django-mongodb-backend
Then of course update the DATABASE
settings to use MongoDB:
DATABASES = {
"default": django_mongodb_backend.parse_uri("<connection string URI>"),
}
Looked easy to me, till I ran migrations:
Well, that did go well ...
So back to the article by MongoDB's team:
I asked if one has to start from their starter template:
Thanks for your question @chrisachinga!
We recommend doing so to ensure certain necessities are incorporated, such as MongoDB-specific migrations and having the settings.py
file already modified for you so Django is using an ObjectId value.
Let me know if you have any other questions, I'd be happy to help.
That was helpful at the moment, oh and don't use it in production ... yet
Thanks for your reply @chrisachinga,
So we don't recommend using our library in production until our GA.
For any issues please open a GitHub issue or Jira ticket!
We appreciate your feedback so much. :)
So we try using the MongoDB starter …
Following the docs:
https://www.mongodb.com/docs/languages/python/django-mongodb/current/
So you create a django project from the template:
django-admin startproject quickstart --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.1.x.zip
Then you update the connection string:
DATABASES = {
"default": django_mongodb_backend.parse_uri("<connection string URI>", db_name="<database name>"),
}
You also create an application from a starter template:
python manage.py startapp sample_mflix --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.1.x.zip
After that, I ran my migrations:
The error was about the DATABASE setting missing the "NAME" value, so I thought this would fix it:
DATABASES = {
"name": "default",
"default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<password>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo"),
}
So that should work, right?
Confusing right? Maybe I rushed into the fix. So I asked on the forum:
Jeffery A. Clark responded with a couple of fixes:
DATABASES = {
"default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<pass>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo"),
}
DATABASES["default"]["NAME"] = "default"
or
(will be available on beta 1)
DATABASES = {
"default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<pass>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo", db_name="default"),
}
The fix is well explained on the PR: https://github.com/mongodb/django-mongodb-backend/pull/249
Did that work? Yes ...
What To Do next?
Testing and comparing performances with other database backends.
What I love about this: Deployment will be super easy and quick, especially that connection to MongoDB is by a URI, and hopefully no other stuff needs to be done ... trying that next 👀👀
This podcast on Django Chat talks about the mongodb database backend on django:
Also another good read:
Top comments (1)
Glad you tried it out for your self! Happy to see you got it working, let me know if you need any more help or direction.