How to get Django Tenants up and running

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello i'm tom and i'm the maintainer of django tenants in this video i'm going to explain what django tenant is and how to get django tenants up and running so first of all what is django tenant if you've ever found yourself needing to create a software-as-a-service django app where you need to separate your clients data from another one of your clients data then django tenants is for you you could get away without django tenants however there's alway would always be a risk across contaminating your customers data you could put an id on every single model that is referencing client data and then do queries to limit each customer's data however this would cause this would be a risk that you would contaminate that you would get cross-contamination of data another way would be to create an individual database for your customers so customer a has a database customer b has a database sounds really good but django doesn't really support this way or you could use postgres schemas so what are postgres schemas postgres screamers are like databases inside postgres by default you always use in a django project a schema called public now with django tenants you can have many schemas but it still belongs to the same database so that is what django tenants allows you to do so how does django tenants know what schema to use there's two ways of setting django tenants up let me explain so one way is to use subdomains so in this example here i've got company1.test.com this would go to the company one schema and this one here would go to company 2 schema so that's how subdomain one way work but there is another way which is to use part of the url you have to give it a specific prefix at the start which there's a setting in it that you put in the settings file and then you can specify what schema so in this example company one and company two for my demo i will be using subdomains like this so we're now ready to create our sample application i will be creating an imaginary sweet shop where each one of my customers sells sweets i will be using docker and pie charm pycharm is just my preferred ide so let's begin so i've already created the django project and installed django tenants using pep so there's a few settings that we've got to do but before we do that we're going to create a new app so i'm going to go to docker i'm just going to run python manage py startup client so we've now got client as an as a new app [Music] the new client app will be used to store all the information about each tenant django tenants require two models to be added so i'm gonna go to the models file and add the two models to do this for ease i'm just gonna paste them in from django tenants help so i'm going to copy that and i'm going to paste it in for the ease of this demo i do not need paid until on to on trial and created on so i'm going to remove those from this this model you will notice that the client model is based off tenant mixing and the domain model is based off domain mixing there are extra fields created when you create a client model and extra fields that are created when you create the domain model these are required so the next thing i need to do is change some django settings so i'm going to go to my django settings file the first setting i'm going to change is the database engine you have to change the database engine to the specific django tenants version so i'm just gonna get that setting i'm gonna put it in here so it's django underscore tenants dot postgres sql underscore back end the next setting that i've got to put in is a database route router so i'm just going to put that anywhere in here so i've done that one and that's django django tenants.rooters.com router the next one is probably the most one of the most important ones which is the middleware so let's check that i haven't already got middleware already in here let me just check i've already got middleware now django tenant's middleware there's different versions of it but i'm just going to use this bog standard one please see the documentation for the differences but it's required to be at the very very top or as high as possible in the list so i've got django tenants dot middleware.main.tenant middleware you might need to create your own middleware to do pacific stuff basically the middleware redirects from the subdomain to the correct tenant so that's really important worth looking at that code so the next one to do is is um the shared app and the tenant app basically there's two kind of apps so we have to split this installed apps into two parts so the first thing to do is create is to change this to share that these are apps that are shared between every single tenant and also available in the public tenant so i put shared apps and now i need tenant apps so i'm gonna copy that and make it tenant apps as well in shared apps we need to put django underscore tenants let's put that and that needs to be as high as possible in the shared apps we need this lot we need to still have installed apps but it needs to be made up of both the shared apps and the tenant apps so this is the line that's required now there's two other lines that are really important that's where the domain um module is listed the domain model and the tenant domain model so the tenant model and the tenant domain model so we need to put these somewhere just put those there and they are in client client right and they go there we also need to put in of course the client model into our shared app right so the next thing to do let's see if it actually runs that's a good sign it does run so now i'm going to do a migration so i'm gonna go to command pro make migration migrations is created to and let's go to and migrate so i've done the migration at the moment we have no tenants created but if you look there is a management command called crate tenant we're going to run that but before we do i'm going to show you there is no tenants created at the moment if you look in schemas in pg admin i've just got really the one called public the cron and this one are system ones so we're just going to move those up move this out of the way and we're going to run crate tenant so the schema name we're going to say is demo name is demo and domain we're going to have demo dot dot local host if you use anything at localhost you will find that that works on your own computer on your local host computer you don't i used to have to modify the the host file but using dot local host means you don't have to modify the host file is it primary yes it is so we should see in here now reload i should refresh rather than reload you have now got a schema called demo the moment you'll find it's only got a few tables and nothing of interest at the moment as we're creating software as a service for sweet shops we need two new apps one which is tenanted and one which is shared so let's create those two new apps python manage dot pwa start app sweet and score shared and another one called sweet lieutenant doesn't need the word tenant on the end but it's quite useful way to distinguish that it is a talented app so that's two new apps done now we need to add those apps to the installed app so i'm just going to go and do that so the shared one will go in shared so sweet underscore shared and in the tenant one suite underscore tenant so the next thing to do is we need to go and add the models to those to those apps so i'm just going to go to the models i've already typed them out so i'm just going to paste them in so this is in the shared one and i want sweet type oh i want to keep that line there sweet type and i want to go into the oven app a tenanted one and i want to put sweet let's move that you'll notice the sweet one has a link to the shared app suite type has a link this has a link to sweet so let's go import that so that's now imported so what we're going to do now is we're going to do the migrations so let's go and do that go in here to python manage dot py make migrations it's created two new migrations one in shared and one in not shed now if we do a migrate you'll find this is quite interesting so it's run both migrations but it's run it twice because we've got once for the public tenant and once for the demo tenant if we had lots it would run the migrations for every single one so yes you could argue if you got 100 tenants it would take could take some time to run all the migrations across all those tents however there is a way of doing the migrations in parallel that's in the documentation so which does speed it up so i'm now going to create a couple of users to log into django admin the first user i'm going to create is the one for the public area and the second user i'm going to create is just for the tenant demo so let's create the normal user so let's go python manage dot put great super user and i'm gonna might as well call that root that's fine and i'm just gonna put a password hopefully i'll let this password oh yes i want to bypass that so now we're going to create one just for the tenant of demo so let's go python manage dot py and we need to run this one crate tenant super user python manage.pri crate tenant super user so you can press a question mark which will list all the schemas out but in this case i just want the schema of demo because i've only got one schema i'm gonna put the username to demo and i'm going to put a password in yes so i've created one super user for the public and one super user for the the demo so i'm now going to bring over pg admin and show you what those users are in here so if we look in here we've got demo which is our demo app and we look at all fuses in here and go view all rows you will notice there's a username in there of demo because that's what we created as if we look at the public tenant and go to all for users in here and go view all data you'll see there's one called root so that's done that part so i'm now going to go and set the django admin up so i'm just going to go into the admin file of sweet shared and i've already typed this up earlier so i'm just going to paste it in so we've got a sweet type admin nothing special about this at all and we go to the other one of sweet and i put in that no i do need the includes so as i said nothing special with this so we're now going to view that to view it we need to split the urls file into two urls files one for public and one for tenanted so what i'm gonna do is i'm going to copy the url as well and put this to urls public and we're going to go now to the settings file and add a couple of lines in so we just need to find in here where the urls file is defined should be in here right here is so we just need to add this line here change that to due to demo dot urls public so we now got this urls file here which is used for tenanting apps and this url here which is used for the public so let's go and run this so the next thing to do is try and load the tenant up in a web browser so i'm just going to bring my web browser over and what we're going to enter is demo.localhost colon8009 because that support i've got remember we put the domain name when we created the tenant to demo.localhost so just press enter on that and congratulations you got to your tenant if we go here and put in a a random tenant name you'll notice the tenant isn't found so what we're gonna do now is we're just gonna put forward slash admin at the end we're going to log in with demo and my password and you'll notice i'm now logged in what i'm going to do now is go to www.localhost so i'm just going to do that and if you look we get an error this is correct by default django tenants will show you a page 404 error if you go to a tenant that doesn't exist you can change this behavior by either modifying the middleware and having your own local copy or in my case what i'm going to do is i'm going to add a setting into the settings file called show public if no tenant is found now we're going to run it now we're going to log in we're going to go to root just put my password in and you'll notice we're now logged in to the public tenant admin you will notice that you cannot modify suite type this is because it doesn't exist in the public apps it only exists in tenanted apps so first of all going to add some sweet types so we're just going to add bar as in chocolate bar and we're going to add some candy you'll notice that this doesn't actually work we're just i'm just going to quickly fix that so i'm just going to go into here go to models and i'm just going to add the right code which i've got here and that is now fixed right so what we're going to do now is go back to our tenanted app and you will see in here you can modify the the public data we can also go here and go to sweets we can now add a sweet we can say it's part of a bar and we'll say it's a chalk and let's do for a moment we don't need to add any more what we're now going to do is we're going to create a tenant we're going to do that on the command line again so we're gonna add another tenant so we're just gonna go python manager crate tenant and the tenant we're going to have is demo two demo two demo two dot local host yes that is primary you can have multiple domains per tenant which is actually quite useful that's why our that's why the tenant table is separate to the client table so what we're now going to do is just create a super user great super great tenant super user and again you should now see two tenants in here so i'm gonna say tenant two i'm gonna open my user code use that i'm not gonna do qwerty as my password it will moan at me yes right so i've now created another tenant if you look if i go to that tenant and log in so with demo2 and qwerty is the password you will notice that i can see sweets however there's no tweets in here because this is obviously this is why the data is separated and you can see but you can see they're still sweet types so if we go in here we can say there's a candy and we can just say pink so that's really django admin setup with django tenants to do views it's really simple all we would need to do is go to the urls file and just add normal views it's no different to any other part of django and queries are really no different either so that's really django tenant set up so i think i've shown you enough in this video to get you going so i really hope you enjoyed this video and if you have please give me a thumbs up and please subscribe to my channel it really does help me thank you very much bye
Info
Channel: Tom's Python and Django
Views: 3,663
Rating: undefined out of 5
Keywords:
Id: TWF7okf5Xoo
Channel Id: undefined
Length: 27min 12sec (1632 seconds)
Published: Fri May 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.