Django Multiple Database Setup Ex1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another tutorial this tutorial is all about using django with multiple databases i do apologize how long it's taken for me to do this this has been on my to-do list for many months now and i've been asked many times so this is a simple getting started with django multiple databases tutorial so in this example we're going to be utilizing or building three databases one for the user authentication and sessions and so on and then we're going to create two more databases one for each app that we're going to build in this tutorial i will cover configuring multiple databases so setting up the databases in the settings we then have a look at building a database router and then we'll go through a very simple example selecting data from the database so we're creating return data using class based views and functions and the last step we'll go through how to set up multiple databases within your django admin area for those not too familiar with django just to give you kind of a visual overview of what's happening here our application or django's up the top here traditionally normally we have one database django knows that database and we'll send that information directly to that database now when we set up multiple databases django will have no idea which database to select so the idea here is that we're going to set up a database router and in that router we're going to describe what databases are available for django and our applications to connect to so at this point we can now extend our normal query so for example collect all the data in the authors table using the default databases chosen when we initially set up django now we have multiple database setups and we configured what databases are available within our router we can now use the using function and describe or define what database we want to use so here for example in this simple example we're using the database other called other so here we had run this query on that database and what's happening here essentially is that information is going into the router and then we can make a decision where that where that database is and then we can perform those actions on that database so hopefully that gives you a general overview of what's happening here of course there's a lot more to this than just that so let's go for our example and start building these applications so i start by building a very simple application called core if you're new to django the commands are here on the screen so let's go into the core and settings now you might already be familiar with the settings file and halfway down line 76 here we see the database being described so this is the default database that the current application utilizes in this example i'm going to be using sqlite or sql lite databases you can change this to whatever database you want to use so here in django this name here is called an alias so this is the alias or name of the default database that the current application is using so by default django will use the default alias or look for the default database when there's no other databases defined so we need to ensure that we keep it here we can just um make it blank and what we want to do now is create some more databases of course if you want to you can just use the default for your default database for example but i'm going to go ahead just creating separate databases with different aliases so the first task is for us to build a user's database so here i've defined the name of this as users underscore db you are going to need a comma after the existing database you can see this calls an error so the engine is just going to be my sql lite or sqlite and then i've named this users so just make sure you've got a new alias a new name and then a new name for your database so with that done let's go ahead and migrate so you can see that the database is improperly configured please supply the engine value so django's looked at the default and it doesn't know anything about this user's db here so it can't do anything about it so now what we need to do is create a router to tell django that this user's db exists and we should use it when we're building the user's db database so go ahead and build a new folder now you can place this absolutely anywhere because we need to define in the settings where this file is but we're going to build a new folder i'm just going to call this routers and inside of here i'm going to make a a new file called db routers so at this point you might also be wondering why i'm calling a router and not a router that's because i'm british and i call it a router so next up we need to define where our routers reside so here in the settings using the database underscore routers variable we've defined routers so inside of our routers folder and then db unscore routers and inside of here we're going to have a class called auth router so for every router or in this case for every class we define within our db routers file we're going to need to produce a new entry here in the database routers so now we're going to build a router for our new database so remember our database is called users underscore db this is our new database it's going to be a sql like database so let's now set up a router for it so first of all create a class class so i'm going to call this all for router remember that's what i connected up in my settings so auth router so this is the link to this new class so here in django the database router provides up to four methods how i describe this next step is we're now going to set up the applications or describe the applications so in this case let's just go for a smaller subset here um so this is the two applications we're going to set up off and content types so we're now going to describe what database access they have and then potentially what um operations they can perform on that database so the first method that we can define is db for read so again just hook up to the or just look up the django documentation this describes this in a little bit more detail but here we're going to describe the fact that these applications can read this database so db for read so we're providing um access for these applications to read this database so here model dot meta app label so based upon the app label um if the app label is in the root app label so i guess what's happening here behind the scenes when for example um let's say that the auth app tries to read a database django will read the app label and then try and determine in the routers here what access that app has to what database so we use that so we can match the app label to the app label that we defined here then it must be agreed that this application can read in this database here so our first method is defining the read access so secondly then it's right so we can read and write a database so the same thing again we go ahead and define our app labels so we've already done up here which can then read to the user's db database so the next method here is allow relation so what we're defining here is the fact that these applications potentially um what they may have for example foreign keys or one-to-one or for example many too many operations that are needed between these tables and we're just basically describing whether that is going to be allowed or not so this is a little bit about the scope here of going into kind of this detail in this tutorial but we've got this method here which we can describe relations so probably a question you'll have later on is relationships between databases so if we have one table in one database can we make a foreign key to another database i guess the initial answer is not really to that question but maybe in a later tutorial we can explore that further so that's the allowed relations method and then finally we have the allow migrate so here we're just determining whether the migration command or operation is allowed to run on the database so this is the database can we run migrations yes okay so let's allow migrations and now we're just defining remember through our app labels up here we're defining what application what app can migrate onto this database and at the moment these two apps when we run a migration will then migrate onto this database so that's the final step so let's go back again and try to migrate so it's likely that we're going to receive an error well we are going to receive an error so let's give it a go again so this time we get a similar problem like we did before uh the please supply the engine value so if you think about the command how would django even know which database to select based upon that command there's no way of it knowing so what we're going to do is supply an additional command which describes what data base we want to migrate and at this point we probably can say that most commands that we want to run now we need to supply some extra additional information for django to direct to that particular database because remember here we're just directing to different databases based upon in this case the the uh labels the app labels so what we need to do here is supply um the the database that we want to run a migration on so let's do that so simply put we just need to describe the database and that equals the database that we want to run a migration on so user underscore db so we run that and we now say the connection doesn't exist to this alias connection does not exist the connection user db doesn't exist okay so you can hopefully see the problem here i just wanted to see you to see that error so that basically just means that we've not selected the right database so it's users underscore db so let's go ahead and do that and there we go we've migrated so as per normal i like to tell you every single time of course i've got sq lite extension installed so that means i can now go ahead and right click on the database open it up and have a look to see what's happened so you can see here we've got a few different tables here um a few different surprises so let's go up so it looks like we've migrated the off group of user um django content type migration and session so it looks like all those tables have now been kind of migrated over to the new database so typically the first thing you want to do is to run a migrate and create create a super user so to do that um if you were to try you can see we've got this engine value problem again so that's indicating the fact that we're going to need to run our database equals and then define our database that we want to create a new user on so let's go ahead and do that and let's just create a new super user and there we go so that was successful so let's go ahead and run the server and work out the next problem so let's just open up a new window let's just navigate to our application so it's working okay so can we get into the admin area so here we have a problem so we've got that same problem again settings databases improperly configured please supply the engine value so again that's telling us that we need to supply this engine value again at the moment the admin app doesn't know what database to use so let's just go back to our code here now up here i purposely took remove some of those items here so these are the apps that have access for read write migration uh allow relation so it makes sense now to think about what other apps we're gonna need here so in settings you remember we've got in the i'm going to use this big window here we've got the installed apps and one of them is the admin so potentially what this means is the admin doesn't have access to this database at the moment so let's go ahead and just add that there as the admin app and let's just go back and refresh and we've still got a problem so when we think about it when we want to go into the admin area we're going to need to build a session so we've also now added the session app to provide kind of access to this database or to route it to this database so let's go back now and refresh and have a look admin and there we go so we now have access to the admin so hopefully you can start to get a flavor of of what's happening here and how we're kind of directing our applications to the this new database and really we're just going to scale from there so admin admin can we log in yeah we can and it looks like everything else is working we have some users and some groups so now let's go ahead and create our second application the blue application i'm just going to call it blue and we have a new app so we're going to need to go into the settings and for some reason i can't get it to remove and then let's go down into the databases here and let's create a new database for this so we're just going to copy and paste and we're going to call this blue so let's remember to also rename the actual database to blue there we go and this is our second application so we're going to need a new router for this so we may as well just go ahead and copy this across and we're going to call this let's just call this blue with a class of blue and now we just need to go into our routers and we can just copy and paste this down and then let's uh call this blue there we go so this this time there's only one app label so i don't need to use this of course i could just do this singular in the actual function there but we'll just use what we've got already so our app label this time is going to be blue um so we're not going to need allow relations we've only got one table and that's absolutely fine okay so now we want to define let's go back into our settings just remind ourselves of the table so this is going to be the blue let's call it the blue db database so this is a database label or alias to access this database of course so the database name is going to be called blue so this is the alias for that database so let's go back in and change this to bluedb okay so that now provides the blue well basically what we're saying here i guess is that we've set up this application and it will have access to the blue database to read and to write that's where i want django to kind of direct the traffic from the blue app to the database this database so we're going to allow migrations based upon so what we're going to do here would try and describe here that when we migrate this application those tables are going to appear in this database only so what we don't want is for all these off content type session and admin tables to be replicated onto this uh into this table into this database so hopefully what we're doing here is we're going to just describe the fact we only want the blue application tables to appear in this blue database so let's go ahead and try that so we're going to need to first of all in our blue we're going to need a model of some sort so i'll just copy something in so just a simple database with title content app name so now we have a table in place so let's run our manage pi file again i mean command sorry so make migrations is a command which we don't need to describe or define what um database to select so because of the dependencies and referential integrity and connections one-to-ones and foreign keys that we may have between different tables and apps then django ought to kind of look at the whole table the whole picture so making migrations um can't be done on a single app so this is kind of a global thing so we don't need to describe or identify the individual database here so we can run that and you can see that we've detected no changes because we need to go into the settings of course just add that in the new app so let's do that again so here we have a new model that's been created so now we can go ahead and migrate and that's going to be the database equals and that's the db so db blue or blue db i don't see i'll record this blue db sorry so that's going to be the blue db there we go so i've applied those migrations and my new database has been created and let's open up the database see what's inside so you can see what we have here is the migrations table and the blue table which is the four fields that we've described as you might imagine from the blue table here okay so clearly we've created a separation now between the django default apps tables and our new application called blue so as you might imagine in the admin let's just go ahead and add this to the admin see if this runs i think we need to restart this server so that didn't start or maybe there's a problem blue admin is not defined okay so that'll be why there we go okay so we've got the blue table looks to be okay so let's see if we can add some data to it and we can so we'll probably be able to delete as well okay so there we go so it's very specific there the signals are being sent to that blue database in order for us to actually update and add data in comparison to these tables here which originate from the user's db table so when we perform actions that at those actions will be routed to that table instead so let's create a third application so i'm going to build another another app sorry called aqua so go ahead and do that so the same procedure here i'm going to go into my core settings uh first of all let's just add it to the installed apps don't forget the comma okay so we'll create a new database for this so this starts to perform slightly differently um this is why it's worth to get going this far there's not really many examples that take you on to this kind of third level uh for you to then experience some of the problems it will that this will cause because yeah let's just carry on uh so we wanted to create a a new table there aqua and then we go ahead and we'll just build a new class so we call this uh the aqua and then we just need to now go back into our new router let's go ahead and just we'll just copy the blue for now so we just need to call that aqua so we need two spaces so this is going to be the aqua app and then we're going to call this aqua db at least i need to double check we did there we go right so hopefully now what's going to happen is the aqua app is going to be migrated into the awkward database um let's just double check our settings to make sure we did that aqua db acura okay aqua underscore db that's the alias that we want to use okay so yep so hopefully now when we migrate so let's go ahead again in our account we just run the same set of models for this let me just quickly grab those so let's just go ahead and run the same model um and let's just go ahead and migrate so we can find that so make migrations so we can see that we're going to be building the the awkward table and then this time we're going to migrate aqua okay so connection doesn't exist because it's the aqua underscore database there we go so let's go back and have a look now at this table or it's database sorry let's open the database see we'll go inside so you can see here we've got two sections in migrations and the aqua table so inside of our table we've got our model so that works okay so again we haven't copied across all the django um models um it's just the aqua models that we're now uh serving from this awkward database so this is where things get a little bit different so we're just going to the admin here we'll copy from blue into aqua right so blue now becomes aqua right can't find the capitals so we're going to reg the aqua table onto the admin area so let's have a look to see what the behavior is when we do that so you can see that it appears but when we click on it um you can see that it's being utilized so let's add something to it shall we okay saved on to aqua so let's go to blue a blue save and that's saved on to what you might imagine blue okay so let's go back into let's go back into our application here um so let's just double check see the data just refresh so i'm just going to have a look at this table and then shows a table to see the data insides and then the blue one probably is the same yep and there we go so we have now have data going into those individual databases so let's move across to the act reviews let's just do this first and have a look at the behavior of the views in this kind of environment so let's go ahead and we'll use the class based views to begin with to use a create view so we'll build a simple form um so here let's just go ahead and we're going to create use the create view to build a simple form we're going to use the title content and app name to build a simple form and then we'll bring in the model here just as for normal for our class so we're going to then need to create a new folder for our templates and then inside of here a new file we're going to call add dot html okay so the only thing that we're going to need on there is the simple form so i'll just put that on there okay so just a simple form right so we're gonna need some urls so let's just quickly route that across so you can have a look at the the final code for this if you're not familiar with uh with this so let's just add the what's the aqua path just copying it just a little bit quicker easy include so i'm just going to link to the acro urls so let's just grab a new file create some more urls for this and then we just need to go ahead and do that so like i said the code is in the the code base um okay so this is run server okay so that's okay so let's go back to our server and we need to go to aqua there we go so we have our form let's see if we can submit looks all right so let's go back into here see if that works um maybe maybe it did i don't know there you go so it looks like it's working okay so let's do the same thing for the the blue app the urls so let's just add the the blue url so we perform the same actions um like we did before we're gonna need we've got the models in place i think we've migrated those um and then we're going to build a new new file called urls and then we're also going to need a template folder inside of there we have a new file called add so let's just uh quickly add all that in so this is the the blue app let's grab that so this is a blue app urls just like that so we're going to create two shortly and that's the urls and then we need to template add so let's uh just add that in okay so we've got the url the view we need to do the view so this view is going to be um not that so we're going to run a a class view here okay so that's the field so we're going to need to bring in the model and the create view okay so let's just run again blue views has no attribute view data that's in the urls so that's the core urls blue urls and yep okay let's just take that out for now okay so let's just go ahead to the blue see if we can give this a go looks to be working okay let's go back into blue refresh there we go okay so hopefully that was fairly straightforward i'm not expecting a few more problems than we did actually get there so um i don't think i need to go any further of course i was going to go to the next step and just grab some data and display it but i don't think we need to do that but uh you get the idea at this point potentially so for example uh in the manual it does go on to talk about manually selecting the database and as i kind of spoke about earlier here is an example here so for example i'm in the uh any app it could be the blue or the aqua app it doesn't matter as long as i bring in the model and then select manually the database i want to use so this is the blue database so here django provides us the option of manually specifying the database that we want to kind of route this query to so that's something that um to definitely look out for if you want to explicitly explicitly define that within your application so the last point to note here although this example didn't need us to go any further exposing multiple databases in django admin particularly when um you want to provide an admin interface like it says for a model on a database other than specified in your router chain so we need to write a custom model admin so it's fairly simple um here we've got some methods that we need to define to allow for example our model to save data delete and to get the query set and so on so that's something else to look at that's the django docs 3.1 and the multi-db dock so by always going to have a look now have a quick read through here just to kind of establish and build upon hopefully the knowledge that you've learned in this tutorial okay so sorry if i sound a little bit surprised i was expecting there to be a few more issues um setting this up i've very just quickly created this tutorial um for you expecting that and have gone and run through okay so we've configured multiple databases we've seen that in action using sql sqlite we've created a router database router to help support the routing of our application to the right database we've gone through the class based view we didn't quite get to the functions um there'd be an example in the code base that you can download and then you've seen us using multiple databases within django admin so again if your database doesn't work then head back to that documentation uh just follow that documentation at the end here and to give you a quick example this would be a quick example of setting up for example the blue database here so i'm using blue get query set save model delete registering that then on the the admin so to do that we just need to pass over our model and our new class
Info
Channel: Very Academy
Views: 11,633
Rating: undefined out of 5
Keywords: django multi database, django multiple database, django, python, django tutorial, djangotutorial, django tut, django 3, django examples, learn django, django beginners, beginners django, django framework, django example, django 2021, python django
Id: g-FCzzzjBWo
Channel Id: undefined
Length: 32min 51sec (1971 seconds)
Published: Tue Mar 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.