Building A SAAS with Django 3 | Django Tenant Step by Step Tutorial ( Part 2 )

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello so right now we're going to start building a sauce with jungle basically this involves adding multi-tenancy to your project so for the demonstration we are going to be using the jungle block application we build during the blog series link in the description also if you check the description you're going to see the link to this final source code which is the sassy blog and in the readme you're going to find steps which you are going to follow to implement this so right now i'm going to go ahead and grab the initial project file for the blog application but you can also go ahead and use your own jungle blog or jungle project so i'm just going to copy the command ssh you can either use https or ssh but since i'm using ssh i'll just copy this command so right now i'll head over to my desktop i'm i'm presently on line expert if you're on windows just make sure you open a command prompt so or you just go and search command prompt or you i think you should not open like command prompt so right now i'm just going to open my terminal and then once i have these i'm just going to paste in that code then press enter and as you can see it's cloning the project like i said on windows you just go ahead open command prompt and then you write the same command if you have an issue just search your command prompt or leave a message in this in leave a comment i'm going to help you out so once that has been done i already have my jungle blocks which have been cloned on my local machine i'm going to close this then head over to virtual cg code which is my favorite code digital and i'm going to go to open folder the folder is on my desktop then i open it jungle blog then now i'm going to click click ok yes so right now i'm set so what you want to do is to make this jungle project it starts or to transform it so it says that is we are going to add multi tenancy to it so if i go ahead on the readme you're going to see that the next step is actually me involves creating a virtual environment if you have never created your own virtual environment go ahead and create it so i'm just going to go ahead and create mine or open a new terminal new terminal from above and i'm going to enlarge that all right i think this should be okay so right now let's see all right right now i'm just going to paste that to create my own virtual environment a vitro environment is where all the requirements or packages needed by your jungle project are going to be saved so as you can see it has created the requirement flow like the environment folder so after that i'm supposed to activate the environment so i'm going to use this command if you're on windows you're actually going to use this command all right once i press enter you can see that my video environment has been activated meaning that all the packages i'm using now are from this video environment as indicated by these brackets so i'm sure i've activated my virtual environment right now i'm going to go ahead and install all the requirements needed by the project come to requirements.txt you can see all the requirements needed by jungle project are presently here so what i'm going to do now is jus to use the command i just copied to install all of them all right so it's going to go ahead and install all the requirements but mine is going to be a little bit faster because i've already installed it in co require um the requirements it's just using the catch requirements are like the previously it's downloaded so all right that has been installed now let's move forward to the next step i'm supposed to make sure that my project is up and running if you're using your own jungle project you actually may have to make sure that your project is running before proceeding so with this my server is running and let me take a look at localhost okay all right perfect my jungle blog application is running so right now if you also had if you are using your own project you just have to make sure your project is up and running so i'm just going to go ahead and close the server ctrl c to close the server and now we are good to go so i'm going to go back to this then continue following this step so what we are going to do now is we are going to install jungle tenants remember we mentioned django tenant in the previous video where i was explaining that it uses the semi isolated approach to implement a size or to add multi-tenancy so i'm going to use hip install jungle tenants which is supposed to install django tenants all right once that has been installed i'm supposed to go ahead and install this other package which is pi in fact the ps scope to something right like that and this package is what is going to be used to communicate with our postgres database remember i mentioned that django tenant like in the semi isolated approach you need to use a database that supports schema and one of the databases super schema is a postgres database and this package is used to communicate with the postgres database actually on windows you might face some errors maybe installing this but if you face any error just don't forget to leave a comment in the don't forget to leave a comment i'm going to get back to you and help you link you to where you can easily solve your program so once i've installed that package i think i'm good to go for now so i'm just going to use this command to save those newly installed packages to my requirements of cx5 all right as you can see it has added those requirements it has added those packages to my requirements of cx65 so right now i'm going to go so basically as a prerequisite you need to have postgres already installed on your machine as you can see here is mine i have postgresql installed on my machine and it's pg admin i'm going to leave a link for the windows installer for pg admin but if you're on ubuntu you have to install the pj admin or postgres database plus pgm yourself so right now let's take a look at where we are so i'm done so now we are supposed to set up the jungle tenant first thing we have to do is to add the jungle tenant middleware that is this is what checks to know which on which database or on which schema is supposed to like check the data let me let me explain that so if i come to call i have my settings and then i have my middleware let's go to the middleware so basically when and your jungle server or jungle application is receiving a request what happens is the request move from from the top middleweights down right down so what we are doing now is we are the jungle tenant middleware as the first middleware why because this middleware now is going to be used to determine which portion of the database is going to be searched for example remember in a semi isolated approach we have one database with several schemas and in each schema like we have schema a schema b and schema c etc so when you receive a request when someone is requesting for something on your jungle application this middleware now is going to get that request and say that okay which from which schema are you looking for data and your request is having the schema oh sorry your request has the schema name which is like i'm looking for request a i'm looking i'm looking for data from schema a sorry so once the jungle tenant middleware knows that your request is going towards schema a is going to go ahead and look for schema ao direct or a request to schema a in that way we have separated the data easily and now we are going to update the database configuration with these new database configuration so if you check here we should be using the default jungle database but now we are going to override that so i'm just going to paste that then number one you have to add the tenant engine engine that is communicate with the postgrad postgres backend then now the name of your database mine is cersei then now the user my own user is omen then now the password and localhost so let's take a look at that so if i come to pg admin you're going to see that i have a database called sassy and that cersei is owned by the user woman and the password is simply password it's on localhost and pod so that means my i'm connecting now my jungle project to a postgres database so once that is done we have connected the middleware that actually look for which tenant or yeah which schema we one data from then now we have the databasing we are going to now add the database router let's see just below it all right i have the thinner database router added now you can check full setups for this like full instruction how to set up jungle 10 and using this link but this basically already shows you how show you how to do it so now what we are going to do now is we are going to create a thinner up this app is going to have all the details about each of our tenants like for example we have customer a we're going to save the data about customer a dell we have customer b we're going to save the data about customer b there so i'm going to use this command python manage.pi start opting on to create a new app so then pull that up all right that should create a new app all right i've gone ahead and created a new app called tinan and now i'm going to create the models for it as shown here so i'm going to go ahead and create the model all right as you can see the tenant up i'm just going to go ahead and create the model now let me just copy that and paste okay let's see what's going on here so if you check the jungle thing and documentation you're going to see that by default we have to inherit from the tenant mixing and the domain mixing so basically retain and mixing contains fields that are going to be used to save data about attendance and then this domain we are not actually supposed to add anything but you're just going to have a link that is going to define which thing we're requesting for data from which you're going to see so i have the user the tenant has a user view remember in a block application we already had a user so my right now we have a user a tendency is related to a user we have the blog name the blog image then we have future updated ad serial views about the tenant description of the tenant is active we're going to use it to activate the sas or the tenant or this activity website then created on an auto autocrate schema means in fact i'm going to explain that when we arrive at the point and auto drop schema so right now let's go ahead i've gone ahead and created the tenant models right now what you want to do is to configure the tenant model so i'm going to copy this then go now to my settings dot wi-fi it's in call settings.wi-fi then i'm going to add this so these are links to my tenon gear receipts so the thin model is at the tenant so in tenant i have a tenant model which is this and then domain model which is what is going to have the link that is specifying the the tenant i want to access so right now i'm ready to smoke so now we are going to set up shared apps and payment apps so basically the shared apps are being stored on the like in postgres schemas right we have the main or the public schema which has like overall information and we have the tenant or the other schemas so basically the shared apps are going to be saved on the public schema of the postgres database so all of these are going to be saved on the public schema why these ones are going to be saved basically just on the tenant schemas so let me just copy all of these and explain again like i said the shared schema the shared apps are going to be saved on the public schema of the postgres database and and the tenant apps are going to be saved only on the specific schemas of those tenants let's go ahead and then i'm just going to paste that so this is my new this is my new shed abso abs definition so i'm just going to clean that yeah so i have these jungle tennant is supposed to be at the top they have having my tenant models and then basic stuff they now these are just stuffs from the jungle blog application series like the nate or the ck editor then my blog app and then what happens is i'm putting the block up here and the block up here the prime knob i'm trying to solve here is because if i don't put the block up here on my public schema like the schema that has general information like the tenant details they didn't the details about the user been saved on my public schema and also i have this block up so i'm putting the block up on this public schema too so that the structure of this of the of my public schema should be similar to that of the thinner schema if not you're going to have some programming error so it's just to solve the issue though it's not necessary normally what you want to do is to put all specific tenant apps here but like i said i'm putting the blog up here to avoid some programming issues so once that is done i think my app has been configured and right now i'll go ahead okay let me just check okay right now i'm just going to apply migrations i mean make migrations let's see this make migration is going to create the different dependent model and the domain model and then now this is just in case this is just in case so i'm just going to apply this to make the migrations to all right use just in case you do not have the migration files for the block so right now i'm going to use now this modify my grid schema command which is actually remember in jungle the first thing you want to do is make migrations which is going to convert your models into like prepare models for it's going to what jungle does it conveys the fire written like for example i have my model i've written so now it creates a migration file that has all the details about my models and then now this file is now going to be used to convert it into sql commands to actually create those tables on our database so after i've applied migrations it has created a migration folder with all the details about the fields i want to create for example big autofill car block name and stuff like so now i'm supposed to apply the migrations that is to actually create those fields on my database so presently in jungle tenant we have a modified migrate command which is my great schema and let's just take a look at my database right now so if i come here okay let me just refresh okay okay as you can see if i come to my database cersei which i'm going to i'm using for the project then i go to schema you're going to see that by default as i mentioned there is a public schema that we are going to be saving all the data about maybe the client and then client and some other specific general stuffs about us as then now we are going to be having now other schemas which are going to be saving the thinner specific data so right now we have only one schema which is a public schema so i'm going to come here now here then hit enter and that should apply all the migrations all right migrations have been applied that means my fields have been created in the database now if i come now here and here i should have tables okay now i have my tenant tenant domain antenna feel like it means it did not the migrations have been successfully applied to my database so as you can see those tables are found on my public schema now let's go ahead now i'm going to create a user the first user is because the creating command needs a user so i'm going to clear this up create a user let's say admin let me just keep that password all right i've created my first user remember i'm creating this because we need an initial user when creating the attendant so now what i'm doing is i want to create my initial tenant in setting up your jungle in setting up jungle 10 and you have to create an initial thing now which is the nanda is responsible for the public database that is all the data about that tenant is going to be saved on the public database it's just like the data about my software service are going to be saved there so that is the first thing is supposed to be the public oh sorry this yeah the public schema that is the data related to myself and that's where all the data is going to be saved so i'm going to call that you have to call that public in fact so you call that public then when i say user1 is refrench the user i just created that so i said that for creates for me to create a schema i need a user as defined here in my tenant domain like my demand tenant model to create for me to create it and then i need a user so i created an initial user so i'm going to use to create my first like the first schema of this yeah so i i'll create my first public code my first schema or the public schema so i press enter i'll just say main block then now no image let's just leave it add it's active true we're going to use this field to be disabling some some how they say some like 10 and when you you create a 10 ions actually activate or disable it in at some point like as with the subscription there now as i mentioned your first thing is always the public for your first schema is always the public schema which all the data are going to be saved which all the data about this scheme or about this thing are going to be saved on the public schema as we saw here so we have the public schema all the data are going to be saved here and those are data about your software as a service so after setting the public schema you also need the local host audit the domain of your website because what django it uses the domain to identify which tenant you're requesting data from on the main like my by default when you launch jungle server the domain you have is the localhost so i'm just going to put localhost here and then we're going to create the other domains of the tenant and then you're going to see how it works so it's primary i'm going to say true and there i think i'm done that i'm going to create a super user and using this new django tenant command which is different from the one jungle uses like managed by crates by the way this one is creatine and spicer so i'm going to use the these to see and then as you can see i have only one schema which is the bobby schema so enter the tenant schema i'm going i'm creating a super user for the public schema so i'm going to type in public username admin one let me see one email none password all right so now i've created a tenants by user then now i'm going to go ahead and run server then check what is happening all right my server is running it means all the steps i've been doing are successful so what i'm going to do now is go to these and refresh all right remember i as i was saying all the data if i come to my settings.5 right now what is happening is since i have the blog application on my public database or on my public schema when i run my application now it gets the data for this flight for this it just run my blog application as it would normally but now we have new features in uh application because now this application is able to support like other users using the same code base so i'm just going to go to admin then we're going to start seeing all right i think i mean one yeah okay all right okay this is the okay it's not at the 10 and okay that was keep is not i've not added the tenant up to my um admin interface so i'm just going to go ahead and add it so i'll just go ahead and copy that and paste so i'm just going to copy it and paste it's a basic jungle all right i've added detail now to my admin interface for example if you want the code for that as i mentioned this is a source code here you can go to the size block go to the tenant app then you're going to have the tenant updating admin you're going to have a configuration which i used to add to my admin interface that's to make the tenant app present on it so now if i refresh all right i have now my tenant app and when i come here i have one which is the published schema the user the owner of the public schema is admin that is the main person having the software as a service then gail like i mentioned we created a block on the public schemas that the public schema should have the same structure as those of our tenants that is to avoid some programming issues so right now as you can see this is the main domain of my main domain for my software as a service so what we're going to do now is we're going to proceed we're going to proceed now and add a custom middle well like i mentioned the middleware is what is being executed once it requests like one when the request is been is entering your jungle up or something like when your jungle application is receiving a request sorry so right now i'm just going to copy this then head over to my call folder and we are creating the middleweight so that in case a jungle a tenant or a customer has not been activated for example what happens is you create your your software as a series someone comes in and signed up once the person signs up you don't just want the person maybe to start using yourself so we are we want to create a middleware that is going to actually activate what is actually the user in case the document the user submitted we're not okay or you want to verify the user or the user gets banned from the system so that's why i click right that's why we are creating this custom middleware middleware dot by oops all right my custom midway then i'm going to paste that as you can see i'm importing the jungle main the jungle tenant middleware for example if i come to my settings that by middleware have the maintenance middleware so i'm coming down i'm importing it there now i'm trying to customize that main middleware into my own custom middleweight then i get now the middleware and i say if the tenant dot is active then like i'm testing since i have the fuel in my 10 and a half is active so if it's active is false that means i'm going to raise the exception tenant is inactive else i return the tenant so that's going to be used to deactivate or activate a user so after applying my middleware right now what i want to do is to add the custom middleware to my middleware folder so i'm just going to go ahead and copy this all right so i'm on my middleware i'm just going to add that below it then comment this out okay so my jungle tenant we see is receiving a request then as i mentioned earlier the request now this first middleware is going to check to know which schema i'm looking for data from oh which similar i'm looking there which team i'm looking for then now this call me the right now is just the media that is going to be used now to activate or deactivate a thinner so once that is done i yeah that should be it so now let's go to our interface oh the server is not running the server stop let's we run the server all right my server is running i refresh the admin interface and like i said we have a main or a public schema this is what this is the main schema or the main this just makes your application work these these are the steps used to like set up your jungle tenant so you just need this public schema as the first user of your database so everything it defines the code domain of your admin or your software and some few details like the main user the owner of the user under the details about your application so what we are going to do now is add actually a new tenant or a new customer but what you want to do in practice is to create a form with a landing page for your application for example if i come to localhost what you want to do is someone comes to locals now you like display um welcome to bloggies create your own blog right away then now once you once the person click on that button now the person has a form where you can fill in the details and for example the form could be something like this but i'm actually excluding the schema name so as i mentioned the user needs to be assigned then the block needs a name then let's see the image of the blog so i'm just going to choose any image all right the blog image let me just click that description of the blog fff the now is active i'm actually going to toggle this or let's see on signing up now the person feels in the form then you want the person to be not activated as soon like you want to verify the person before activating the account then now this is where the magic starts coming like these are domain now so what happens is this domain now is going to be used to find this specific schema like for example this is the schema let me just give now the name for the schema like this is what this is what jungle or this is the schema name for this specific user or for this specific block so his schema name is block one so once we are going to apply this you are going to see this being created here on a schema we are not just going to have only public below we also have that schema which has the data about this specific block and the data is going to be completely isolated from all the others so i have my schema name the user the block name i have an image then i'm just going to go ahead and create this and let me explain what was what is happening now if you come to your jungle lock you're going to see that migrations have been applied that is it's been created it's creating this section of your database as you as you're seeing is creating the section applying all all of this stuff so it's actually applying the migrations and creating it to that demand so it does this automatically because in our models.wi-fi we said autocrate schema that is when i create a thinner it auto creates it automatically creates a schema on a database and once i did it this auto drop schema means once i delete the schema it's going like once i just delete it then i'm sorry it's going to now delete that schema so let's go ahead and check that number one if i come now baby to my localhost and say blog okay blog one.look i was all right as you can see i received the information saying that tenant is inactive which is done by this a custom middleware so tenant is inactive it returns it that the tenant is exactly nazi so your tenant has signed up and has created his own block then now you are supposed to verify his account before now activating it so this shows that this is what happens your jungle tenant made the world that is the first middleweight this first meeting will receive this url and then what he's going to do now is he's going to use this first word or the first this thing the sub domain since django 109 is going to identify the schema which you're referring to all the schema which you want data from using this first word so block one.localhost what is going to do is going to take this and then know that what you want from the schema from which one data is a blog point schema and if i have just the localhost it knows i'm getting data from my public schema if i have the block one it knows i'm looking back for data from my block one schema so let's come to a database like pg admin and reload all right after reloading as you can see now i have a public schema and block one schema this is where all the data about that specific thing are going to be safe about the specific block so right now i'm going to go ahead and activate that block so i'm going to activate it then save it so i've validated my user and my user now can actually use this block application so if i come now here and refresh that's it i have a blog application for this specific user and i also have a blog application for myself or just to use in case so this is the block application for a specific user this is the other block application to show that this is completely unrelated let's go ahead and create an article on my main block so on the only block in the scheme and let's see if i go ahead let's see user first name blah blah blah all right i see that i've created an auto headline or it is an article featured and there you go let me just publish all right i've saved it so i have now an article on my locals or the main or the public blog application so if i refresh now i should have now the data loaded here or the blog article as you can see now i have a block article on my main domain and if i go now to my sub domain which refresh that of the client or the customer that is block one and refresh i should have no article because the client has not created any sql so now with this you have several instances of like you can have several instances of your blog or your jungle project running using different schemas or for different users so that's basically so what you want to do now is you have your main page that is having all the data about your information about yourselves someone comes in then create the the blog or something like pretty soft like its own usage for the software once he has created his own blog he can now access it on the subs on the sub domain of your website that is basic the basic way of creating like a sauce in such way that now each data from this and the other user is different so basically you basically have a size now which you can manage from your admin interface and stuff like so if you even check your you're going to see that these so as you can see the user for block one the localhost also has its own admin interface and what you want to do now is to create a super user for it so you see go to your command desktop let's see for example we stop your silver so once the user has created an account now you have to create now a super user for that account so now i'm going to create it then if i list the different schemas available i now have two schemas that is a block one and then the localhost so i'm going to use block one then let's see [Music] block one admin and there you go i have created the super user for the block one admin interface now if i come here and all right as you can see now i've successfully logged in to the admin interface for that block one and now let's go ahead and just create an article same thing and make sure they are different so i have the auto as you can see for example if i come to this admin interface if i come to this admin tab is you're going to start on creating a block already created an auto which the auto does not exist on the other admin interface so when i come here so the user now can be managing his own stuff about the blog while you manage your own stuff about your blog and with that said you have yourselves or you can also create like a reaction and that is going to be using this just to get the data and then you create a customized user experience or a customized interface for specific users so i'm just going to go ahead create a new auto and that's the straight line let me use this as you see okay and with that i think i have a new article future publish then i have a new article now on my blog one localhost and if i come now i should see there [Music] all right block one the localhost now has his own admin in the um has its own blog and same thing here we have two instances running and you can go ahead and create as many instances as you want so with this said you have your own sas you know how to create a search basically and nice having you here thanks for watching and don't forget to subscribe for more
Info
Channel: Academy Omen
Views: 1,401
Rating: undefined out of 5
Keywords: django, tenant, tutorials, saas, multitenant, git
Id: QU70PbJuAUo
Channel Id: undefined
Length: 39min 59sec (2399 seconds)
Published: Mon Aug 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.