How to Connect MySQL database with Django Project | Beginners Tutorial 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I will show you how you can connect your Django project with mySQL or any other database so this is the boilerplate code of Django app that I just created in my pip file I have installed Django net if we view the settings.py file in our MySQL underscore DB folder you can see that the default database that the Django project used is sqlite but if we want to change that database then we need to change some settings here first of all I need I will run this code to show you that it is working fine for running the project I will write the python manage.py run server command it will show you that it has 18 unapplied migrations that I need to apply if I click the server it will show me that the application is successfully running so so first of all I need to make all these migrations other than that I am creating a new application so that we can also create some custom tables in it so for this purpose Let's Learn Python manage.py and start app I am naming this app as users so as you can see it has just created our app users so let's register this app in our installed apps just write the name of application here in my case it is users so in my users app I will create a URS folder so if there are any URLs that will land in this folder in my reviews in my reviews file let's create a simple review name it as all users that will return all users registered in database and it return a simple response we need to import this from Django http so let's return that returning our users we need to import this from django.http so this is the simple view that I just created we need to register this view in our urls.py file so from Django Dot URLs import path and from and import the views of the users we need to create a URLs patterns list so this is the URL that I created when the user hit the slash user's endpoint I will showing it as that returning our users so let's create the model for this users so for creating the motors we need to create a class and I am naming it as users for the case of Simplicity I am inheriting it from the models.model and register some attributes like the user can have the email it should be the email field with the max length of 30. for password feed let's make it as jar field and with the max length of 50. let's create an Android motor so that I am naming it as person and same as that it contains to field like first name elastic Dot chart field and I am giving it max length of 70 and for last name it is also a Char field with a max length of 70. so these are the two Motors that I created for the user for the user model we need to do one more thing after that we are ready to migrate over tables we need to register the URLs of the user's app in our main URLs files so let's first include the import the include from django.urls so that it can include the URLs of users app when the users hit slash user's endpoint I will display the endpoints the URLs present in my urls.py file of users I can register the user's URLs by writing the user's dot urls so now let's run the server first python manage.p wire and server so it it has no errors when I hit search users it will show me that a returning old user so the user's application is successfully installed in our main settings.vi file so we need to make all these migrations first of all I am using the mySQL sqlite database that is the default database of the Django application we need to make the migrations first by running python dot python manage.py make migrations it will make our migrations for us it contains all the default migrations and two migrations and through our custom migration like it created the user's model and the percent model so now we need to migrate these changes by running python manageable migrate command so it has applied all these migrations so if I open the database and so if you know thus sqlite is a file based database it is node viewable in our vs code we need to use some third party a software to view it I am using that DB browser for sqlite if I open this database in SQL Lite you can see it contains all the tables like it contains the user's person model and it contains the uh the user's model as well so we need to change the sqlite database to some other database like MySQL mongodb posters for doing this we need to go to the settings.py file and we need to make the changes here like here it is using sqlite3 as the engine for database and the name of the database is db.sqlite3 that is this file we need to make the changes here for using the mySQL database with the Django app we need to make the changes here like django.dp dot backends we need to write MySQL here for the name of the database we need to write the name of the database we are trying to use so let's create a new database I am opening my MySQL workbench now let's open any of these connections I am using this Django and Django Dash practice and note that the user of this connection is root and it is running on Port 3006. let's open this and we need to create new mySQL database here for getting new misk database you need to click on create a new schema and name your database say like I am using is at testdb you need to click on apply and apply so it will create a new database for me so my database is successively created with no tables in it so we need to write the name of the over database here like I am editing it as test DB let's verify the name first test underscore DB okay we need to add some more options here like the user of the user of the connection or the database the password of the connection that you they have to use used by initializing the connection The Host the port number and some other options as well so these are some of the mandatory options for the user I am naming it as root as my user is root for password I have set result password one two three the host of my connection is the localhost that is one to one two seven Dot 0.0.1 and my port is 30 0 6. so after doing all these things in our settings.py file we need to do one last thing so we need to install a package and that is our third battery Library you can view this by searching the PIP install a py MySQL if we open the and this official documentation it will show you that how you can install this package for installing it in globally you need to write this pip install py MySQL as I am using the virtual environment so I will install it by pip EnV PP and we installed py by SQL so it will install the py MySQL package in my virtual environment it will take some time and install the py message SQL in my which one so as you can see the py MySQL package is successfully added in our packages list the purpose of this package is to make the connection between the MySQL server and over Django app we need to do one last thing before we are ready to run over server in the underscore underscore init underscore underscore dot py file we need to import this client library and we need to call its function that is installed as installed as mysqldb so now we are ready to run our server when I run my server by running the python manage.p wire and server command you will see that it contains some new migrations because we just changed our database to mySQL so you can see that we have 19 unapplied migrations so we need to make apply these migrations again and then you can see that you will see that these migrations and the tables will be added in our mySQL database so let's apply these migrations by running python manage.py make migrations this will apply all the migrations and then we need to run the migrate command so it will apply all these migrations and created all these tables in our mySQL database so we need to refresh it refreshold so these are all the tables ended in our mySQL database so in this way you can add the mySQL database in your Django project so if you want to use some other database like postgres or some other database then you can also do this by similar way you just need to create your database on postgres on on my uh or on any other database and then you need to change this engine by writing just like here postgres and then you need to write the name of database user password host and the port so in this way you can connect any SQL database through those Django project thanks for watching
Info
Channel: Coding with Kazim
Views: 24,692
Rating: undefined out of 5
Keywords: mysql database, django tutorial, sqlite3, connect mysql to django, database connectivity in django
Id: 5g_xIwxLSJk
Channel Id: undefined
Length: 13min 34sec (814 seconds)
Published: Mon Jun 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.