How to use Django with MongoDB Engine

Learn Django with Mongo DB. In this tutorial, you will learn how to integrate the Mongo DB database. Configuration in Django. And the migration of Django Provided tables into the Database of MongoDB. Like other databases such as MySQL and Oracle. However, the Mongo DB is no SQL Database.

Requirements :

pip install django
pip install djongo

The above commands will install the package of the latest Django on your local machine and as well as the Django package. Link for djongo

Note: This djongo will degrade the version of Django on the local machine. For example, if you are using Django 3.1.4 it will be changed to 3.0 or 2.9.

Download Mongo DB compass app :

You can directly download the mongo DB Compass application by click here.

Firstly Create a Django Project :

python -admin django startproject project

This command will create new project.

Navigate to Project folder :

cd project #projectname 

This will change the current directory of the terminal to the project directory

Secondly Creating app with Demo app :

py manage.py startapp demo

Create a demo app with the django project to maintain the code.

Create Student Model :

from django.db import models
class Student(models.Model):
       name = models.CharField(max_length=200)
       roll = models.IntegrField()

In this step, we are creating a model with the name of the Student with the two fields that are name and rank. However, the name is character field this will create a column with the name under the student table. And the roll is an integer field this will also create a new column under the Student table in the Database.

Firstly Configure Database in Settings.py :

DATABASES = {      
'default': {          
             'ENGINE': 'djongo',          
            'NAME': 'your-db-name', 
         }  
}

Therefore you will get connected to Mongo DB with your Django project. Up next we need to migrate the database as well in the next steps

Finally Commands after configuration :

py manage.py makemigrations 
py manage.py migrate

After these commands, the created table Student will also resemble the demo database which we have created inside the Mongo DB. Database. And compass application for accessing the Mongo DB database on the localhost server.