The Good Tech Companies - Django MongoDB Backend Quickstart: Everything You Need to Know
Episode Date: May 21, 2025This story was originally published on HackerNoon at: https://hackernoon.com/django-mongodb-backend-quickstart-everything-you-need-to-know. Follow the quickstart to crea...te a Django application, connect that application to a MongoDB deployment, and interact with the data stored in your database. Check more stories related to programming at: https://hackernoon.com/c/programming. You can also check exclusive content about #django, #mongodb, #python, #database, #mongodb-backend-quickstart, #creating-a-mongodb-cluster, #install-django-mongodb-backend, #good-company, and more. This story was written by: @mongodb. Learn more about this writer by checking @mongodb's about page, and for more stories, please visit hackernoon.com. Follow the quickstart to create a Django application, connect that application to a MongoDB deployment, and interact with the data stored in your database.
Transcript
Discussion (0)
This audio is presented by Hacker Noon, where anyone can learn anything about any technology.
Django MongoDB Backend Quickstart. Everything you need to know, by MongoDB.
Interested in diving into our Django MongoDB Backend integration?
Follow along with this quickstart to create a Django application,
connect that application to a MongoDB deployment, ensure your deployment is hosted on MongoDB Atlas,
and interact with
the data stored in your database using simple CRUD operations.
What is Django?
Let's first go over what Django is.
Django is a high-speed model view controller framework for building web applications.
There are a ton of key benefits of utilizing Django in projects, such as rapid development,
a variety of services to choose from, great security, and impressive scalability. Django prides
itself on being the best framework for producing flawless work efficiently. As
we'll see throughout this quick start, it's incredibly simple to get up and
running with our Django MongoDB backend integration. Pre-requisites. To be
successful with this quick start, you'll need a handful of resources, an IDE.
This tutorial uses Visual Studio Code, Python 3, 10 or later. We recommend using 3, 12 in the environment, a MongoDB Atlas cluster.
Create your MongoDB cluster. Please follow the steps to create a MongoDB cluster on our free tier cluster, which is free forever.
Please make sure that the network settings, a correctly set up for easy connection to
the cluster, and that a secure username and password have been chosen.
Once the cluster is configured, please make sure if the connection string is in a safe
place for later use.
Load the sample data into the cluster, and in the connection string, specify a connection
to the sample database we are using. Do this by adding the name of the database after the hostname, as
shown in the code snippet below. Now that our cluster is ready, we can create our virtual
environment.
Create your virtual environment. Our first step is to create our Python virtual environment.
Virtual environments allow us to keep the necessary packages and libraries for a specific project in the correct environment without having to make changes globally that could impact other projects.
To do this, run. Then, run. Once the is next to the directory name in the terminal, the virtual environment is correctly configured. Make sure that the Python version is correct, 3, 10 and above, by double checking with.
Once the virtual environment is set up, we can install our Django integration.
Installing Django MongoDB backend.
To install the Django integration, please run the following command from the terminal.
If both PyMongo and Django are installed in the environment, please ensure that the Pymongo version is between 4.6 and 5.0 and that the
Django version is between 5.0 and 5.1.
When correctly run, this is what we'll see in our terminal.
Once this step is complete, our integration is correctly downloaded along with the dependencies
that include Django and Pymongo required for success.
Create your Django project.
We're all set up to create our Django project.
This template is about the Semea's the default Django project template, with a couple of
important changes.
Ite includes MongoDB specific migrations, and the file has been modified to make sure
Django uses an object ID value for each model's primary key.
It also includes MongoDB specific app configurations for Django apps that have set.
This library allows us to create our own apps so we can set.
Run this command to create a new Django project called.
Once this command is run, it will show up on the left-hand side of the project file.
Once you can see the project, it's time to update our database settings.
To do this, go to the file under the folder and head over to the
setting. Replace the with the specific cluster URI, including the name of the sample database.
Now, we can make sure that we have correctly installed the Django MongoDB backend and our
project is properly set up. Make sure to be in the folder and run this command.
command. Click on the link or visit http://127.001-8000. On this page, there will be a congratulations and a picture of a rocket. Now that we know our setup has been successful, we can go ahead
and create an actual application where we can interact with the sample data we previously
downloaded. Let's dive in, creating an application for our, sample underscore mflix, data. We are
first going to create our application. Head into the root directory of your project and run this
command to create an application based on our custom template. The template makes sure that
your file includes the line, default underscore auto underscore field equals, Django underscore
mongodb, fields, object id auto field. This ensures that instead of using the original Django for IDs, we are using MongoDB's specific
Object ID feature.
Once you create your project, we are going to create models for our movie, viewer, and
award data.
Create models for our movie, viewer, and award data.
To create data models, all we have to do is open up our file inside of our newly created directory
and replace the entire file with the following code.
The model here represents our collection and stores information about various movies.
The model, on the other hand, represents the collection and stores important user details
for a movie streaming platform.
The model represents the embedded document values that are stored in the model.
Once this is done and the file is saved, let's create views to display our data.
Create views to display our data.
To display data from the database, we'll add views to the file.
Open it up from your directory and replace the contents with the code below.
Here, we are displaying a landing page message and information about the N models we configured above. Once we have finished this section,
we are ready to move on and configure urlsfor our views.
Configure URLs for our views. To be successful in this section, we need to create a new file
called insideo4 directory. This file maps the views we defined in the previous step to dedicated URLs.
Copy the code below into this new file. Once that has been copied in, we can go ahead to our file
and replace the file's content with the code below. Once we've finished replacing the file's content,
we can create templates to properly format our data. Create templates to format your data.
First, create a subdirectory inside of the directory.
Once this subdirectory is created, create a file inside it.
We are going to copy the following code to format the movie data requested by the view.
Create another file in this same subdirectory and call it.
This template formats the user data that is requested by our view.
Copy the following code.
Now that your templates are in place, we can include our application
inside our project, including our application inside our project.
To do this, head over to the file nested in the directory and edit
the section to look like this.
Once that has been done, we can create migrations for our new models.
Create migrations for your new models.
We want to create migrations for our and models and apply all these change to the database.
Head back to the root of your project and run the following commands.
Once the migrations are created, we have a basic Django MongoDB back-end application.
We can use this simple application to interact with our database.
This means running basic CRUD operations on the data.
So, let's go over how to do that.
Write data.
We are going to be working with a Python shell,
so head back into the projects root directory and bring up the shell with this command.
Once you're in your shell,
we can import the necessary classes and modules for creating an object.
Do this by running the code below.
Now, we're ready to insert a movie into our database.
We can do this by running the code below to create an object that ready to insert a movie into our database. We can do this by running
the code below to create an object that stores data about a movie named, including its awards.
This object actually stores incorrect data about the movie. The value is listed as, but
the correct value is. Let's fix this by running the code below. Now, let's insert a viewer
into our database as well. We can do this by creating a object that stores data about a viewer named.
Run the following code to do so.
Let's delete a object.
A movie viewer by the name of, Alice or Thorn, is no longer a member of the movie streaming
service.
To remove this viewer, run the code below.
Once that's done, exit your shell using this command.
Ensure we are in the directory, and start the server using this command.
Great.
We can now go and make sure our model was correctly inserted into the database.
Do this by accessing the link, http://127.0.0.1-8000-recent-underscore-movies.
This is what you'll see, with our latest movie right on top.
Let's check our model, as well.
Visit the link, http://127.0.0 1-8000 viewers underscore list.
There will be a list of 10 viewer names in our database.
Abigail Carter, Wilb at the top, and the viewer, Alice or Thorn, will have been deleted.
Once these steps have been completed, documents in the sample database will have been inserted and edited.
Now, we can query the data inside our database.
Reading back our data, open up your Python shell again, and please make sure that you have imported the necessary modules from when you first created the Python shell back under the step, write data.
Once your shell is ready to go, we can query our user's collection for a specific email.
We want to query our collection for a user with the email. Do this by running the following.
This will return the name of our user. Now, let's query our movie collection for runtime values.
In the same shell, run the following code to find movies that have a value that is less than.
This will return a list of matching movies, as seen in the screenshot below.
Once this step is finished, feel free to run queries on any data stored inside the MongoDB cluster.
Let's create an admin site.
It's possible to create a Django admin site so that users can edit their data straight from a web interface.
Let's first create an admin user. From the root directory, run the following code. The terminal will ask for a username, email address, and password. Enter the following information below
to create a user with specified credentials. To enter the admin site, run the following code. Access the site by visiting http://127.001-8000-admin.
A login screen will appear. Enter the name and password that were created previously to log on.
Here, the following information is presented, and users can edit their project authentication
configuration through a selection of the arrows in the table. Let's edit the data in our sample collection. Remember that this is represented by the model,
which is not associated with the row as shown in the admin panel.
Head to the file and paste in the code below. Refresh the Django administration site,
and it will be updated. Now, we are able to select a viewer object.
Do this by clicking on the row of the table to see the list of viewers, as seen below.
At the top of the list, click on.
From here, we will be able to see the end.
Here, we are able to edit the information, if chosen.
Users are able to edit any field and hit the button to save any changes.
Great job, you have just completed the Django MongoDB back-end quickstart.
In this quickstart, you created a Django application,
connected your Django application to a MongoDB deployment,
ensured your deployment is hosted on MongoDB Atlas,
interacted with the data that is stored inside your cluster,
created a Django admin page.
To learn more about Django MongoDB backend,
please visit the docs and or repository on GitHub.
This article is written by Anaya Rezinghani, developer advocate at MongoDB,
and Nora Reedy, technical writer at MongoDB.
Thank you for listening to this Hacker Noon story, read by Artificial Intelligence.
Visit HackerNoon.com to read, write, learn and publish.