How to Connect Django Project to Already Existing Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is kennebunk from cambridge tech so in this video we are going to look at how we can integrate an already existing database into a django application so many a times if you are designing a jungle application or a jungle project with every application or with every app that you create it has a model and the model is where you create your database but there could be a scenario where you have an already existing database you'd want to attach it to your application and that's exactly what you are going to look at so i have a database by name my school and i have a table student so this is some data i generated i mean some dummy data i generated and this is what you want to integrate into our application so under normal circumstance this we are supposed to design as a model but in this case it's almost as if we are reversing it so this has already been designed and we are reversing it and integrating it into a django application so what i have over here is i've already created a project which i call school projects then i've also created an app which i'm calling dashboard over here and i think i also have my template set up so that's just about it so when i go into settings.pi let's register the app over here so you are gonna say dashboard dot apps dots dashboard config then what i also have here done is i've already linked up the templates folder this that i created over here here i think as far as settings.pi is concerned we're almost through with this so let me save this and let's run our application so what i'm going to do is i'm going to type in python manage dots by run server so if i open this up there's a jungle web app the default settings it gives us so now what you want to do is first of all we want to change the database settings over here we want to use mysql so in order to use mysql then we need to also change some parameters in here so the first thing we are going to change is the engine so instead of sql i3 you are going to choose or you are going to say mysql for the engine so we are saying jango.db.backend mysql then for the name [Music] this is actually going to be the name of the database and as we have it here the name of the database is school so i'm going to type in school over here then there are some extra parameters i need to specify then i'll come in here and specify user and user is going to be roots small letter roots okay so this is all in the jungle documentation but a very quick way to get all this information is when you are using mysql you can actually get some of this information over here so as you can see we have user roots at localhost okay so the root is a user and the we have a parameter we're going to specify as host and the host is going to be local hosts then we are going to proceed to provide a port so i'm going to type in ports and also um one of the ways you can find your ports if you don't know the default ports you are using is i'm actually running the mysqli through my exam server so there's mysql and we have this number over here so this is actually the port and that's three three zero six so the port is going to be three three zero six i'll put a comma over here then um the last parameter you need to pass in here is a password so this is supposed to be a password now as far as this database setup is concerned i don't have a password in here so i'm going to leave it blank but depending on what you are using you may want to or you'll be required to have a password so a dev server is work it's running now if i'm to save this it's going to reboot and if there's no problem it is still going to run but if maybe in the course of typing i make a mistake so for instance for the user instead of roots i'm saying rule you can see that clearly it says ssd9 for user rule okay at localhost it was expecting roots all right so when i do routes back it is still going to work now if i'm to make a mistake and instead of the name of the database i write in something else you can see that it says so unknown database show or whatever that pronunciation may be so you just have to pay attention to your dev server and if it catches an error then clearly you have some issues here and there then i think yeah for the port 2 if we make a mistake it is supposed to show us exactly so can't connect to mysql server yeah so definitely any mistake that we make as far as these parameters are concerned we are going to be prompted and it's important you have this setup over here all right so now we are confident that our jungle application is able to kind of connect to this database we have over here school so what i'm going to do is um we have this setup okay so i'm going to go into dashboard and just go and create a view so that we can see things from that end so i'm going to create a function based view i'll have this as index and i'll pass in a request then i'll return render then the first parameter i need to pass here is request then i think the name of my html file is going to be index.html so i'll save this and this is my index.html over here now the next obvious thing i need to do is to give it a url route so for the url route i think let's make things very simple let's go into the projects urls and write this over here so first of all i'm just going to come in here and see from from dashboard dot views i'm going to say from dashboard i want to import views over here so that i can have access to the views in here then i'm just going to do path and there's going to be a base url so or base directory or roots so i'm just going to say views dots and i can have access to index over here all right so this is just about it there's a simple demonstration i want to do so obviously the assumption here is you have some knowledge as far as working with jungle and databases concern we are now re best engineering everything we are supposed to do over here so what i can do is i can actually come in here and come and refresh and i think our work is going to work all right so this is what i have as far as the template is concerned so for the template i have a navbar and this table over here and this is exactly where we are going to put in our data so this is basically what we are seeing in um index or from index.html so you have bootstrap setup over here we have our nav and this is our bootstrap stuff so i'm not going to go so much into it i have other videos of bootstrapping jungle so you can check them out i'll leave the link in the description below so now we are about to go into the main work and that's connecting this database here but first of all let's do a migration because we would definitely have to do so so i'm going to do python manage.pi and let's migrate so that we can have everything set up over here all right so our migration is done and if i'm to go back into my sql database and come and refresh you're definitely going to see that we have these by default jungle database models in here okay so we have all these in here and we also have a student here that we want to integrate into our system all right so the next thing we need to do is to find a way of bringing our model okay because as it stands our model jungle does not recognize it okay because you don't have any model in here so if i'm to come into my dashboard app models.pi is empty i don't have anything over here now in order to like i said reverse engineer this for django to automatically create our model for us i'm going to run in the python command over here so i'll do python manage dot pi then i'll say inspect eb to like inspect database and i have this angle bracket over here then i'll do models dot pi so let me shrink this a little bit so that you see everything over here so this is what i type python money.pi inspect db and the angle brackets over here like the greater than sign and i'm saying models of pi now this is going to create a new model.pi somewhere here okay inside the main directory so i run this and here we have model.pi now if i want to open this up and let me close this you can see that django automatically generates every model we have in that database we specified and that database is cool okay so django creates every model in there so as you can see this is the other group we have the odd group permissions and these are exactly what i'm referring to so the other group the all group permissions the auth permissions auth user and everything in here and that certainly is going to include our students um database okay increase our model for it so i think just about the last thing we have right here is going to be our student db so this is created for us and we see some of the things but at the very top we have some few instructions over here so he says this is an auto generated jungle model module now you have to do the following manually to clean it up rearrange the model's order make sure each model has one field with a primary key set to equal to true make sure each foreign key has a one-to-one um field has on delete set to a desired behavior and things of that sort all right so what i'm going to do is since you're only interested in this student's model i'm just going to do a control c so i'm copying this and inside my dashboard app model.pi i'm just going to bring this over here so that it will behave as if we designed it ourselves over here so when i save now i have my model set up for me so i can go through the process and do some database queries to actually see the data on our interface and that's certainly what you are going to do next so i can come in here into my app views.pi so i'm going to say from the current models i want to import students and this is the current model as you see right and i've copied students over here so you are good to go so inside our view function over here i'm just going to create i have a query set over here so i have the query says student is equal to then obviously i'm doing students dot objects dot all all right so i can have a context over here and let me just grab this so i'll do a ctrl c paste it here and my value is going to be this and finally i need to pass in my context over here so i'll save this and we are in business so i can come into my index.html and come and type in some um python logic over here so the first thing i want to do is to take off the rest of these data over here and only leave one rule all right so if i'm to save this and come back over here you have just one rule so that our looping is going to take care of what we need to do so what i'm going to do is obviously going to put up a for loop over here so with my template tag i'm going to say for for students in students then i'm going to end the loop over here i'll do n4 alright so basically i think yeah so what i'm going to do is i'm going to say student students dots student.id all right so let's yeah so this is a model i have student or id which is going to be an auto generated the way django handles its auto generated id okay so you can just have access to that id the next one i'm going to do is the student id so i'll just do inside my template over here bring out my template tag then i'll do students dots the student id right here then i have first name last name and address okay so i'm gonna do student.firstname i'm going to do student.last name over here then i think i'm gonna have students dots i think city and finally student.address all right so i think yeah that's the order in which we have so we have let me just close this okay so you have the student id which should be showing up over here the first name they come to the last name the city and the address alright so after saving this if i come here and come and refresh we are definitely going to see the data from our database all right so let's just pay attention to maybe the first three rules also so for the first names of the first three rules we have um delilah gretchen and hermann and that's essentially what you have over here delilah um gretchen and herman gretchen okay gretchen and herman all right so every data we have in our database is now going to reflect over here then obviously let's create a super user in here so i'll do python manage dot spy create super user so i'm going to type in admin and admin at gmail dot com and my password is going to be one two three four all right i'm just going to accept this so that i can proceed so i can now log in into the admin page and i'll type in my username and my password all right so this is our default setup okay now we can obviously come into uh admin dot pi and come and register up over here so i'm gonna see from [Music] the current models i want to import students over here then i'm going to say admin those sites dot register and the model that i want to register is students so i'll save this and clearly if i come to come and refresh i have students who are here i can click on this and as you can see we have student objects 25 student objects one and things of that sort and that's because inside our models dot pi what django created for us we didn't put out any strange representation so these are things we can sort out ourselves okay and i just wanted to show you so that you wouldn't have the impression that maybe something may go wrong this is a normal thing that we are quite used to so i'm just gonna say um in order to have the strange representation i'm going to pass in self over here and i'm just going to return i'll say self dots first name so when i save this and come back over here we are certainly going to see the first name listed over here we could have also inside admin.pi um dealt with a list group so i'm just going to have a class and i'll say student student admin and i'm going to inherit from [Music] this admin which is already imported for us over here so admin dot model admin then what i can do is i can do a list display the list display is certainly going to take a list or a tuple so what i can do over here is i can have first name last name then let's say yes city so first name last name and city and in order to get this working properly i just have to come in passing the student admin class i just created over here so just by doing this if i come to come and refresh are definitely going to see the first name um last name and city okay so i can add a new um student so i'm just typing some random student id number here and this time around i'm going to add my own name so kenneth and the last name is going to be brony then the city is going to be accra then address i'm just going to pick up this random address so when i save this you know i have kenneth added to a database if i'm to come here to come and refresh you're definitely going to see kenneth over here as you can see the reverse or the other is in this direction we can also do a reverse of the order but then as you can see we have connect brony acrylic and this is my address i mean the address i choose all right so this is just about it and um i think um some of you find this content very useful because um it is not always that we have to build our model right from scratch but then um i'll suggest you don't have to just play around some of these things it is only prudent when you actually need it that's why or that would be when you would actually have to do some of these things now kindly subscribe to my channel if you haven't and stay tuned for content like this i'll be uploading short videos of how to solve some peculiar problems and also building applications from scratch thank you very much and bye bye
Info
Channel: KenBroTech
Views: 57,091
Rating: undefined out of 5
Keywords: KenBroTech, Web Development, Front End, Back End, Programming, connect an already existing database to django project, connect django project with mysql, how to connect django project to an already built database, django models, automatic models for django
Id: CkYvpKZyEqI
Channel Id: undefined
Length: 24min 0sec (1440 seconds)
Published: Tue Apr 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.