Skip to main content

Connect and Configure MongoDB in Django Project

 Django, a high-level Python web framework, is well-known for its robustness and versatility. While Django natively supports popular databases like PostgreSQL and MySQL, you may want to integrate a NoSQL database like MongoDB for specific use cases. MongoDB’s flexibility and scalability make it an excellent choice for handling unstructured data. In this blog, we will walk you through the process of connecting MongoDB to a Django project, enabling you to harness the power of NoSQL in your web applications.

Prerequisites:

Before we begin, ensure you have the following prerequisites in place:


  1. Django installed on your system.
  2. MongoDB database server installed and running.
  3. Basic familiarity with Django project structure and database concepts.
  4. Virtual Environment, this is optional but recommended. You check our blog here.

Note: For this tutorial, we are using our basic skeleton project for Django. You can also download the project from here.

Step 1: Install Required Packages – Djongo

Start by installing the necessary pkg to connect Django to MongoDB. We will use the “djongo” package, which acts as a bridge between Django and MongoDB:

Ezoic

pip install djongo

Step 2: Configure Mongo Database Settings

Open your Django project’s settings.py file (located inside the main project folder) and navgate to the DATABASES setting. Replace the default database configuration with the following code to use MongoDB:

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'CLIENT': {
            'host': 'localhost', # Replace with your MongoDB server address
            'port': 27017,               # Replace with your MongoDB port if different
            'username': 'your_username', # Replace with your MongoDB username (optional)
            'password': 'your_password', # Replace with your MongoDB password (optional)
            'authSource': 'your_auth_database', # Replace with your MongoDB authentication database (optional)
        },
        'NAME': 'your_database_name',
    }
}

Ensure you replace 'your_mongodb_host''your_username''your_password''your_auth_database', and 'your_database_name' with the appropriate values for your MongoDB setup.

Ezoic

Step 3: Test the Connection

With the MongoDB configuration in place, it’s time to test the connection between Django and MongoDB. Start the Django development server:

python manage.py runserver

Visit http://127.0.0.1:8000/ in your web browser. If everything is correctly set up, you should see the Django default landing page.

As like in MySQL and PostgreSQL, you dont need migrate database while using mongo db in python.

Conclusion:

Congratulations! You have successfully connected MongoDB to your Django project, enabling you to harness the power of NoSQL in your web applications. MongoDB’s flexibility makes it an excellent choice for scenarios where unstructured data handling is required. With Django and MongoDB working together, you can build scalable and dynamic web applications tailored to your specific needs.

Comments

Popular posts from this blog

How to host Django Application in AWS using gunicorn & nginx in Production

in this post, we will see how to use    nginx  with    gunicorn  to serve    django     applications  in production.  Django is a very powerful web framework and ships with a server which is able to facilitate development. This development server is not scalable and is not suited for production. Hence we need to configure gunicorn to get better scalability and nginx can be used as a reverse proxy and as a web server to serve static files. Let's get started  Step 1 - Installing python and nginx Let's update the server's package index using the command below: sudo apt update Copy sudo apt install python3-pip python3-dev nginx Copy This will install python, pip and nginx server Step 2 - Creating a python virtual environment  sudo pip3 install virtualenv Copy This will install a virtual environment package in python. Let's create a project directory to host our Django application and create a virtual environment inside th...

Mongodb-CheatSheet

  All MongoDb commands you will ever need (MongoDb Cheatsheet) In this post, we will see a comprehensive list of all the    MongoDB  commands you will ever need as a MongoDB beginner. This list covers almost all the most used commands in MongoDB. I will assume that you are working inside a collection named 'comments' on a MongoDB    database  of your choice 1. Database Commands View all databases show dbs Copy Create a new or switch databases  use dbName Copy View current Database db Copy Delete Database  db . dropDatabase ( ) Copy 2. Collection Commands Show Collections show collections Copy Create a collection named 'comments' db . createCollection ( 'comments' ) Copy Drop a collection named 'comments' db . comments . drop ( ) Copy 3. Row(Document) Commands Show all Rows in a Collection  db . comments . find ( ) Copy Show all Rows in a Collection (Prettified) db . comments . find ( ) . pretty ( ) Copy Find the first row matching the ob...