How to connect Django Project to Multiple Database Management Systems (Part1/2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is kenneth brandy from cambridgetech and welcome back to the channel so it's been over a month since i uploaded a video and especially because of some of the things i do so for instance i'm a graduate student and i have some obligations with with regards to academic work so fortunately for me i'm through it um some of the things that i needed to do that took a lot of my time these days so i'll be uploading as frequent as i used to you so this video is actually a throwback so i started my channel somewhere in august 2020 so it's been a little over a year now as you can see august 2020 so it's been a little over a year now since i started in fact the first video that i posted which i've actually taken off because um looking at it now it's kind of embarrassing so i've written the video and i'm going to upload it in this particular section so in that light the project that we are going to work on is how we can connect a single jungle project to multiple database management systems all right so many a times um we build an application and we connect it by default django gives us the sqli3 but we can also use mysql which i think i've demonstrated in some videos you can also use postgres but in all those examples it was almost like a single database management system handling the whole application in this particular example we are going to see how we integrate multiple data management systems and like i said this is a throwback to one of the first videos i uploaded on youtube and a remake of it in a better way for better understanding right now let me also add if you're a complete beginner and you don't know much about jungle i have a series over here i did a couple of months ago and that's the jungle inventory management system um it starts from part one and it ends at part 22 and it takes you pretty much from the very basic to an intermediary level and definitely if you are able to follow along you you definitely get yourself acquainted with web development and using jungle as a whole so for the purpose of this um project we are actually going to use or we are going to follow through with the jungle documentation and trust me everything you would ever think of is provided in the documentation so what you normally would want to do is you just have to google anything so in this case just google jungle multiple databases and you see a link i'll leave this link in the description below and it's going to lead you up over here so i'm going to open up vs code and i'm going to open my terminal and i'm going to cd into my desktop and i'm going to create the jungle project over here so i'll do django admin that's project and the name of the project is going to be multiple eb ms which is database management systems so i have this and the project has been created so i have to navigate through my files open folder go to desktop and i have this showing up over here i'll select this alright so and i'm quickly going to run through some few things over here so once again we are connecting multiple database management systems onto one single project and here we have in settings.pi you can see that we have the database settings over here and this is where all the magic is going to happen so if i'm to go back into the documentation you can see that we are going to use what we refer to as database routers okay let me come back we are going to use database routers and this an example that has been provided over here so you can see that the default database has been set to now okay or an empty string over here as you have it or an empty dictionary actually so what we are going to do is i'm actually going to change some few things over here i'm actually going to bring the defaults up this way and i'm going to add a trillium come over here okay so what you have is nothing different from what we have over here except that they are using uh for the user's database they are using mysql and we want to maintain sqlite for the first database and for this one i'm going to call this auth eb so this is going to be odd db later you're going to understand why but let's come down here and actually see um when they actually spoke about the routers and how to use the routers they give an example over here and that's where they use the old db you can choose any name of your choice but then i decided to go with odb and over here today are using mysql for their database remember without debut using sqli3 that's the default database but before we can set everything up and let me just show you this if i'm to open up the terminal and let's say run python manage dot pi and i say make let me do migrate we are definitely going to run into some problems over here and it's pretty much straightforward it says database is improperly configured please supply the engine value check settings documentation for more details so clearly by default django is only going to recognize the default settings it is all you are supposed to apply but then we are changing so few things over here and we need to inform jungle but before we do so there's something we need to look out for over here so this is going to be something we are going to work with as a database routers and as you can see we need to include this in our settings.python finally in the settings file we need to add this okay so let's just copy this i'll do a ctrl c and inside settings.pi maybe at the very end over here i'll paste this over here all right we are going to change some few things in here and before we do so let's create a folder so i'm going to call this folder routers sorry routers and if you are not seeing this icon over here don't worry i'm using an extension that's pretty much changing a lot of things i do over here so don't worry so inside routers i'm going to create in a file and i'm going to call this db underscore routers dot pi is supposed to be a python file all right so we are going to come in here and we are going to see what we put over here but clearly we don't need this for now so let's get rid of this so there's a list okay and this is linking up to our database routers and it says the path to the router so as you can see it says that we should substitute path dots to dots with the actual path to the module that we want to connect as a router so clearly that's going to be this route test okay so it is going to be routers dot and it's going to be dot db underscore routers dots now we are going to leave this auth router as a default thing and we are going to apply the settings over here so what this basically means is the db unascor routers is actually pointing to this folder routers and inside these folders or inside these folder routers it is accessing the db underscore routers.pi module over here and inside of it we need to have this auth router class and that's what we are going to write over here but the documentation um actually handles everything we want for us so let's just copy this auth router class and let's just copy this so i'll do a ctrl c and a ctrl v over here right so let me just collapse this so that we see everything showing up over here so clearly the let me save this so the comment over here pretty much sees a lot over here so under this class we have some methods over here and the main thing we have over here is um some of these comments are very good in terms of trying to tell us what's happening so what you have over here is a router to control all the database operations or models in auth and content types application so now the question is where is the auth and the content types application now if i'm to go back into settings.pi and pretty much come in here we are going to see that by default we have these installed apps and admin is one of them auth is one of them content types we have sessions messages and moving forward if we install or if we create any new application we need to come and register it over here good so these are or this is where the auth and the content type is actually coming from okay now in order to simplify this and also make this very simple we are also going to add the admin so this is going to be a dictionary so i've added admin over here and i'm also going to add sections so i'll do a ctrl c and i'll put it over here good so this is saying that it is a root app labels okay so this is the name of the apps that we want this auth router to handle and these are the methods we need so we want to read the data okay we are seeing that if the model dot and that's called meta dot app label in cell dot router label okay self dot route up labels if it is inside then we want to return the auth bb now the auth db is actually what we specified as an engine over here this is auth db so basically what this method is actually trying to implement is as far as the reading of data is concerned okay if the label and these are the content of the label is inside if there's some model which is inside of this label as specified over here then you want to return on to use the auth db table which is um in this case going to be a default sqlite3 database you are going to look or see how this will work and there's also a method for writing okay so we can read over here we can write over here we can also allow all relationships okay so um whether there's a foreign key a one-to-one relationship this is also how we are going to handle it and we can also allow all migrations so for the auth um or for the main database we want to leave one database that's actually going to handle everything a database is supposed to do then perhaps in other databases depending on how we are building our application we can actually have a database that's going to only permit users to read users cannot write data into the database okay you can also have a database that's not going to allow relationships so that's definitely going to be some kind of security measure depending on the kind of application that you would want to implement so having said that now we should um open our terminal and run the migration so i'm going to say python manage.pi migrates and we are still going to run into some problems over here but we are going to um sort ourselves out okay so having done this now python manage.pi migrates now when we come back into the documentation and this at the very top over here it says because we are applying multiple database okay within our system django may get confused so in running a migration if we say python manage.pi migrate we need to specify the database that we want to migrate and in this case or the example shown over here is users so you can see we have users over here we have customers over here that's basically what we have over here as the engine we have users and we have customers over here so in our case you will realize that for now we only have authdb so what i'm going to do over here is i'm going to do a dash dash database and i'll do a sequel to and i'll type in auth underscore eb so i'll press enter and now we have our migrations going through all right so we are good to go now let's create a super user so that we can have access to the backend so i'm going to type in python manage.pi and create super user now i'm going to type in admin and just about now we have an issue or we have an error okay and it is repeating the old problem that we had and a way to get around it is when we type python money dot py create super user we need to also do a dash dash and we are going to specify the database that you want to create the super user for as far as that particular database is concerned we want to do is equal to and once again it's going to be auth db so now let's proceed we can have admin and we can have our email address and i'm saying admin gmail.com and our password is going to be this and now we can have access to the back end so before we do that let's start our server so i'll do python manage.pi run server so our server has started and let me do a control and click on the link over here so let's quickly go back or into our back end and let me type in the credentials i used in registering all right so clearly we have a user registered over here good now this user is actually saved into my sqli3 database okay the sqli3 database actually and that's what we are using for this particular auth db database engine now let me open this up and um over here so i'm going to use a database browser over here and i'm going to open up this database so i'll just go into the projects which is supposed to be multiple db and just select this and here we have everything showing up over here now if i'm to go into browse data and select the user model that's exactly what you have over here add me and there's a email address all right now let me register a new user and that's very simple to do i'll just click over here and i'll type in my name over here kenneth and i'll type in a password all right so i'll save this and and it has been saved okay you have connect over here we don't have anything for email we just ignored it okay now if i'm to come here and come and refresh you can see that we have kenneth showing up over here so everything we save as far as the users are concerned are going to be saved inside of the sqli 3 database now let's move on to see how we can add additional database onto our system and let's come down here and it's very straightforward so over here all that we need to do is to probably just copy one of these as you see it's over here so i'll do a ctrl c and come inside here inside our databases let me just close this and let me just paste this over here all right so bear in mind we need to add a trillion comma at the end of every database in order to make it work all right so the first thing we need to do is to give it a name and for this one i'm going to say school school underscore db and the name of the database is going to be school okay this is going to be the name of the database or which we are going to create now in the case of my sqlite or in the case of mysql database my username is root or my user is root and my password is um i can show my password over here for obvious reasons but then i have a video i'll leave the link in the description below and i'll tag it just at the top over here and that's actually um explains how you can use environment variable to save some of these things so i have my password saved in an environment variable so in order to access that i'm going to come in here and run an import so i'm going to say import os so that i can use the os module to have access to my environment viable password i'm going to say os dot environ dot get and what do i want to get i want to get db and that's called password all right so you don't see my password in plain text and i'm also going to um let me come back do a comma over here and i'm going to add in a port for the pot is going to be an integer and i think it's going to be 0 0 3 3 0 5 yes yeah 3 3 0 5 now let me open my sql in workbench all right so it's 3305 for the ports and let me type in my password over here all right so these are all database i used for the example and now i'm going to create in a new one so i'll click on this and i'm using the database school the name of the database is school so i'll click on apply and click on apply over here so the school database has been created for me right over here i'll close these ones and i cannot concentrate on school all right so let's see what happens next so now that we have this database and we have our credentials and everything looking good over here the next thing we need to do is to come into our database routers over here and let me just copy this now do a control c so now there's a list oh we can actually do this and i'm going to put out a comma over here and i'm going to paste this right over here so this is going to be something i'm going to change the name and i'm going to call this school router so this school router is actually going to point to the school router okay or the school database we just created and all that i need to do now is to go into the db routers and go and create a class of school routers but in order to simplify things let me just copy this over here so i'll do a ctrl c and i'll paste this over here and change the name over here to school router now one thing i'm going to do is i'm just going to get rid of these comments over here all right so everything looks good as we have it over here so we have the screw router showing up over here so now look at this we have the root app labels okay we haven't created the app yet we are going to create the app now let's go into our terminal and over here let's create a new app so i'll do python manage dot pi and i'll do start and let me call this upschool all right so we have screws showing up over here and let's go into our settings.pi once again i'm going to register school inside our installed apps so over here i'm going to say school dot apps dot school [Music] config all right now this is the upper way of doing it you can just type in school over here but then if you do it this way you save yourself a lot of future issues all right so we have um our app registered over here so now if you have to come back over here it says about the roots app labels and the app label over here is cool and we don't need anything for auth content types admin and session so what i'm going to do is i'm going to clear this and i'm going to type in school over here all right so this is going to be school now for this database you want to do some reading over here and we don't want to return this we want to return the school underscore db okay remember that's what we specified for the database engine over here good and we'll change this as well and then finally there's the last one over here all right now for the purpose of this demonstration we're not going to use um any relationship in the school database so for allow relationship method we are actually going to get rid of it so let me just play this all right now i'll save and let's apply the migrations and see how this is going to work out so first of all i'm going to type in python manage dot pi and i'll say make migrations or make migrations i need not to specify any module all right and yeah clearly we haven't specified any database in here so let's quickly come here and say we want a database of class and we are seeing screw and you have models dot model and i'm going to have name and we are going to say models.chat field and i'm going to specify a max length of android and [Music] let's have address is equal to models chart field once again and max length and for address i'm going to say 200 and let me just have a strange representation over here so double underscore str double underscore and i'll pass in solve and i'll return self dot's name over here all right what i need to do obviously is to come into admin.pi and register the module so that you can have access to it at the back end so i'll do or say from the current models i want to import school then what i can do over here is i can say admin dot site dot register and i want to register school all right so now we can make those migrations over here good so now you can see that the system is now ready to accept the school migration so now let's do python manage dot pi and for the migrate we're gonna do dash dash database is equal to and this time around we want to do the school db all right those could be will now be migrated all right so we are good to go so now let's go back into our back end and see how things are beginning to look over here so let's refresh and we have schools over here so when i click on this i can now come in here and type in let's say university of ghana as a school and this is going to be the address um university of ghana um legon and when i save this and see that this has been saved okay if i should come here and go into my school database okay and let me open this up and go into my tables and inside my tables let me open this up okay now i have the school database showing up over here now if i want to click on this and say i want to select the first thousand rules now you can see that you have this data entry inside of this database and if i'm to come in here and try and check we don't see anything of school database showing up over here because we apply the migration to a particular database engine which is the mysql all right so now that we have this running let's work on adding another database and that's going to be the postgres database so once again let's go into our settings.pi let me shrink this up a little bit and let me just copy this i'll do a ctrl c over here and i'll paste this over here and let's change this to let's say let's call this club db and the database name we are going to call club and the engine over here is going to be postgres ul for postgres so now for postgres the user is postgres those are my settings and i'm using the same password over here again so i don't need to change this and i'm going to change the pot to 5432 all right so let me open up postgres and have it with pg admin i'm using quite an old one i think the new ones have a better interface alright so i'm going to open this up and i'll type in my password over here and inside of this an example i used earlier on so let's create in a new database and this database the name is going to be club so after clicking on ok we have claps showing up over here all right so now let's go inside here and let me just collapse these ones they are getting too many and let's create a new application so let's do python manage dot pi start up the name of the app is club all right so we have club over here now let's come in here and register club inside our installed apps so i'm just going to duplicate this so i'll do a shift alt and the down arrow key to duplicate this now just select scroll over here i'll do a ctrl d to select the next screw and i'll just type in club all right so i'll change this to a capital c so i have this setup done for me now the next thing i need to do is to come inside of um our db our database routers over here but once again let me just copy one of these and i'll do a comma and i'll paste this over here and this is going to point to a class of lab routers which we are going to create very soon so inside our db routers.pi all that i need is to copy this i'll do a ctrl c now paste it over here and i'll change this up to club routers and the name of the app like we just did is going to be club and the db we specified it's also going to be club db but then we are just going to do that let me see the database yes we have club db over here all right so you can also come in here and do club db and finally we can do club db over here all right so this is going to allow migrations and remember this is very important because we definitely need to allow migrations for you to work okay and this right is obviously important because you want to add in data all right so this is going to be it and let's quickly come back to school i mean the model over here and just copy everything over here so i'll just do ctrl c and coming to club models.pi and let's just paste this over here and let me change this to club all right so i have club over here let's go into admin.pi and register it over here so i'm going to say from the current models i want to import and i want to import cloud and i'm going to say admin dot sites dot register and i want to register club over here so now [Music] let's run migration make migrations over here all right so now clap is ready to migrate so now let's go to migrate and instead of doing school underscore db we are now going to do club and that's called db so press enter now we have this migration over here good so now let's go back into the back end let's refresh now you have clubs showing up over here let's click on club and add in some data so i am a national fan so let me put us now and let me see london so when i save this we have has now saved in the class model and the class model is linking up to postgres and let's just have a confirmation over here so let me just come and refresh and inside class let's go to schemes and public and we have tables and we have the club over here so when i come in here and say i want to view the first hundred rules now you see we have arsenal london showing up over here if i'm to come into um mysql there's no way and if i should refresh okay a number of times i'm not going to see club um [Music] database or the club tables showing up over here okay it's not happening because i haven't applied those migrations over here and we are connecting to different database management systems and in that same vein if i'm to come in here and try and refresh as many times as i want there's no way i'm going to see a database so it's like we are separating data and at the same time integrating it in a jungle application so it is going to be the first part of this video in the second part of the video i'm going to um further demonstrate how we can apply database queries and how we can add data from the front end now if you find this tutorial very interesting there are a couple of ways you can help me grow my channel and you subscribe to cambridgetech and don't forget to hit on the notification button so that anytime i release a video you will be notified also share this video with friends and family who find this content very useful at cambridge we say learn programming you can do it and also don't forget to ask any question if you really do have one thank you very much and bye bye
Info
Channel: KenBroTech
Views: 494
Rating: 5 out of 5
Keywords: Django Models, Django Multiple Database, SQLite Django, Postgresql Django, MySQL Django, How to connect Django Project to Multiple Database Management Systems (Part1/2), sql, django models, django apps, django project
Id: dBiC9XKf4pc
Channel Id: undefined
Length: 36min 58sec (2218 seconds)
Published: Wed Sep 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.