Connect MongoDB with Django project using PyMongo | Complete Guide to MongoDB CRUD Operations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I will show you how you can connect mongodb database with your Django project as we know mongodb is one of the most famous no SQL database but before implementing the connection between mongodb and Django project you should know these things first let me create a txt file where I will explain you some Concepts let's name it as test.txt I will delete it afterwards so if you you should know that Django officially support only SQL databases these are the postgres postgres SQL Mario DB MySQL Oracle and sqlite it has the default built-in sport for sqlite that you can see here that DB dot sqlite it is default available Django also has large number of third parties packages that support in that you can use for connecting your SQL database video Django project but if you want to use mongodb with Django Django does not have any built-in sport for using nosql database the first possible package that most of the tutorial on internet follow is jungle but I will not recommend using jungle due to its various drawbacks first is jongo is incomplete it is an experiment if you want to use mongodb for some basic operations like red operation then you can use jungle but if you want to use mongodb for some professional juice and jungle is not compatible second is the issues in is GitHub repository if I open the GitHub repository of jungle you can see that it has a 319 issues at the time of recording this video and one more drawback is we can only use Django version three point 3.0.5 with jungle if we want to use jungle we only we need to use only the 3.0.5 version of Django then how can we use mongodb with Django project first of all we need to bypass the built-in way of using the Django wirem just like in settings.py we need to comment this out and use some other way for connecting the mongodb database with our Django project we will be using a third party package known as py we will be using this package for connecting the uh connecting the mongodb database with our Django project first of all let me show you my code we don't longer need this txt file so I am deleting this so first of all I have created a project using the Django admin quad you can see the commands in method first of all I run Django admin start project and the name of my project is mongodb connection then I jump to that folder init after that I run the pp and we shall command to initialize my virtual connection or my virtual environment after initializing my virtual environment I I installed the Django and this py in my virtual environment you can see these packages in my pip file you can see that Django and py is already installed in my virtual environment now let's run this now let's start my server you will see that it is running fine so it's mean that we can use the py with our Django project so it has no compatibility issue so first of all we need to do some things first of all I am creating a new application by running the command python manage.pwell start app and I am naming it as percent as you can see the person application is successfully created let's register this app in our installed apps by just writing the name of app after that we need to connect the mongodb database with our Django project for this purpose I am creating a new file and name it as DB underscore connection.py in this file I need to import my package that is py and first of all let me create a new database let's open the mongodb compass is opening I need to copy the URL first where my database is running so this is My URL where my database will run let me copy this URL first and make a variable known as URL and paste that URL let's connect to this connection and create a new database click on create a new database and name your database as test underscore you need to give a collection name here I am giving it an apis user so let's create a database so as you can see the database is successfully created after initializing this URL variable we need to we need to call a function known as client and pass that URL you need to store this in some variable I am naming it as client and I am ready to uh connect my database with my Django project so let's call this client and pass the name of the database that you I just created I have named it as test underscore I store it in my in a variable called DB let's run the server to see whether it contains some error or not so as you can see it has no edits so my and connection between the mongodb and the Django project is successfully created in my models.py file of the person's app let's import this from debut connection import the DP by using this debut connection we can register our first model you need to pass the name of the model here I am passing it as person so this is my first collection you need to store this in some variable I am naming it as Personal Collection now we can perform any of the operation with this person collection by using this variable in our views.py file let's import this Chrome dot models import person collection and let's Implement some of the views Define index this will be written first of all let's import the HTTP response for Django http HTTP response so we will return this response from every uh from our app review let's return H1 episode ending let's perform some action with our debut collection so I am implementing a new view view that will add a person for the case of Simplicity I am creating my person object here it contains two Fields first name and Lasting first name is John and last name is Smith by using this DB collection we can add any records in our database you can also take these variables from this request object by using This Personal Collection you can do various options operations like you can Implement delete many you can Implement delete ones you can Implement find and some other as one I will use as insert one that will insert this single record in my database and return a response that the new record is added new person is at it let's Implement a last view that will give us all the questions let's name it as guitar person it will take request person's collection dot find this will return us all the person's present in database let's return this person's list lastly we need to register the URLs for these views let's create a url.py file and register our urls first let's import the required imports from django.urls we need to import the path and from our granted views we need to import the views create a URLs patterns list and register all our URLs here like when the user hit the road URL that it will be slash person we will be showing that the views.index when the user call the add person view will be showing the we will be adding a new user in database and when the user hit the slash show or get all and we need to place a comma here then we can will be showing the views dot get our percent so these are the views for this uh person app lastly we need to register this view in our main urls.py file first of all let's import the include and import and Implement our review urls include persons.urls person.url that will be the name of my URL let's stop the server entity and the servers to see whether it contains some error or not so it should be urls so you can see our server is successfully running let's open the server in our root of the application it is showing nothing when I hit the slash person you can see that the application is running first let's let me show you the database whether you can take the person motor load so it does not contain the person model yet when I call the slash add a route you can see that it has added a new person in my database let's refresh it you can see the new person collection is added and it contains one record let's add a new code here it should contain a new code now so it contains the two records with its unique IDs and lastly let's show let's hit the slash show endpoint it seems like I I did a little mistake while returning the response so in our get all person view we need to return the HTTP response note the URS list directly let's rerun our server and call this endpoint again so you can see it will return us as the URL so we can also return this response as Json response so in this way you can connect your mongodb database with the third Django project and Implement any number of views as we want thanks for watching
Info
Channel: Coding with Kazim
Views: 11,571
Rating: undefined out of 5
Keywords: django rest framework, mongodb, mongodb with django, pymongo, database connectivity in django
Id: oUIjHQMBdD4
Channel Id: undefined
Length: 13min 31sec (811 seconds)
Published: Wed Jun 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.