FastAPI and React Full-stack Application: Pydantic Models using Tortoise ORM (Database design)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube welcome back to my channel so in today's tutorial i'm going to go ahead and start uh the first tutorial of building our api right so in this tutorial we're going to go ahead and uh see how we can be able to go ahead and uh start building our api so the first thing we need to do you talk about building this application using react intel uh fast api right as the back end and the react as the front end so these two are going to go ahead and actually design our database that we'll be using for api so design a simple database of about of two tables and then i'll be using identic models and stuff like that if you're new to a fast api don't worry we'll go over all these concepts so let's get ahead and actually start our virtual environment so i've opened up my terminal here and i'm going to navigate to my desktop so i'm going to pack one directory and i'm going to create a folder here i'm going to call it first uh api react tutorial just like that and i'm going to change the directory into that so you can create a folder on your desktop or anywhere you want and then make sure that you navigate into that directory so now that you're into that in that directory it has an empty directory you can see i have no content in there so uh now that i have that i'm going to go ahead and actually create a a python virtual environment so you can install all our dependencies so we're going to go ahead and actually say python so how to create a virtual environment in python is actually very easy what you have to do is simply type in uh python type in as i type uh python python 3 or python and then v e and m so rather and then v e and v of v e and v v e and v okay so make sure that you type it like that so uh this is this v and v could be anything that you want but these other commands must be as they are and if you're on windows make sure that there is a different way of creating a virtual environment on windows so if you're on windows make sure that you google how to create a virtual environment but if you're on uh if you're on uh on on a linux like me or a macbook then that is how you create a virtual environment but for windows it's a bit different so just go ahead and google and see how you can create a virtual environment for windows it's actually very simple uh so now that you have done that let's go ahead and actually create that virtual environment so now that the virtual environment is being created we need to go ahead and actually install a couple of libraries but before we do that let's make sure that your environment is activated so i'm just going to ls you can see we have created we have a folder in there called venv so now i'm going to go ahead and actually activate this i'm going to say source vnv which is the folder for slash uh pin for slash activity so this is going to activate my virtual environment for me so let me just make sure this is actually v e n and then press enter you can see now my virtual environment is activated and is the name of the virtual environment is between parentheses right before the other lines within the terminal so now this shows that our virtual environment is activated now let's go ahead and actually try to install the libraries that you're going to be using so i'm going to go to fast api visual documentation this is the first ap that you're going this is the ap that are going to be creating with all these endpoints so now with in this we're going to just design the database for this api so i'm just going to go to fast api and you can tell this is the installation that they tell you to install so you need to copy this copy this go back to your terminal then i need to paste this so i'm going to paste that in there and then press enter this will go ahead and actually install that for me so to paste you have to press ctrl shift and v if you're on the linux i don't know about the other operating systems but if i mean you can just also use the mouse if you want so yeah so you can see now we have flask api a fast api already installed and for the database we need to install something known as a tortoise or ram someone using tortoise orm so for tortoise orem is very fast this app was a sync in python i think away so let me just go ahead and actually uh let's get started so let's see how we can install install it uh let's search for read so let's say go to get getting started and let's see this is the installation process so i'm going to go i'm going to install this i'm going to copy this uh copy that and i'm going to go back to my terminal and i'm going to do uh paste it again this will install for the tortoise orem which is going to be the database that will be using for our api okay so all that is installed i'm going to click this and i'm going to open uh i'm going to open a visual studio code so i'm going to press uh code uh it's just going to open informing visual studio code open views recorder whatever editor that you're using uh yeah so feel free to use any editor that you want pycharm uh visual studio code anything that you want it's sublime text just feel free so now that we have the let's go ahead and actually go and create a our app so let's call this app dot bye so in here this we're going to uh our flask up is going to reside so first of all need to import it so then say from uh first api input you need to import uh first uh a b i would watch out for the casing so from first api import first api and i'm going to go ahead and create the app so we're going to say first it's just like very similar to um to flask right if you have ever used flash for those first hour view values flask is the same situation right so now that we have that we don't need to say if name is optional you can go and it's begin to create routes you can say app dot dot get so this is how you create the routes in first api so instead of using upload route and flag in flask in air first if you use app dedicate okay so now that we have the let's go and actually create an api a simple uh route called gate and i'm going to say diff and i'm going to call it index and then we're going to simply go into return return a dictionary which is going to be uh let's say a message and then you're going to give it a data of sorry i press the wrong key so close this and then i'm going to say uh hello hello so i'm just going to return that so now you can go ahead and actually begin to run our api and then if you go to localhost output output 8000 it's right by default first appearance at localhost at put 8000 you're going to be gonna get back this uh the form of json so it does it does a jsonifying uh path in the backend and like a flask api you have to do it yourself this one it takes care of it since the first api is is just built to deal with apis in python right so let's get started so let me just go back to terminal clear this make sure i'm in the direct directory and i'm going to say uh make sure my virtual environment is activated and i'm going to say uvicon and then the name of the app the name of the file and then the name of the app the instantiation of the app so if you this is a let me just show you this is the name of the app of the file you call the file app right the file naturally in the code because uh update pi right and then for our instantiation of the app you should call it pi we also call it app okay so rather so if i come in the terminal i type in the name of the file which is app and then colon and then the name of the instantiation of the app and then i can also specify an optional reload so you don't have to reload every time we make changes so uh i i get an error so now okay the reason why i get this error is that i forgot one more thing we need to install another library called uvicon so we need to say pip install uh uv con so uv gun is just a server that uh fast api uses to run uh the web application so this is going to run on this server called uv con so now it's installed let's go ahead and actually try to run that again and you can see our server started on localhost output 8000 so i'm going to go to localhost this you can see here i'm only on localhost at about 8000. so let me just this was for the last tutorial of the introduction so let's go to uh refresh that you can see we have uh we got a detail not found so let's go back to our app and see where we're getting that error so we are saying update and then you're saying for slash this is going to be uh the gate in an index so we're going to be returning hello port okay so now let's try to refresh this uh we're getting back not not found so let's go try to go to first slash docs and then we're getting first api we're getting more aspects so let's make sure see why we're getting that error so uh using first api instantiating up the decade up that index and then you're simply going to return uh we're going to return this dictionary of hello world so uh if i check you're getting back a 404 not found so yeah guys i have it exported my era so i forgot uh the uh that it's actually a decorator so this is actually a decorator my bad so i forgot that era so sorry for that it's actually a very silly area that i made so now that we have the uh the uh the decorator there so we can now go back to our front end right uh we can go back to the front end and then try to refresh this you can see we get back hello world so just to make that with my error i forgot the decorate and i have to take a look at the documentation and i just realized that i forgot they created right here okay so my bad sorry for that uh let's get continuing this thing let's close some of these uh windows so that we can have a space room for that so now we just that's a simple api that you have built and that's how you can return uh return a simple hello message from an apa so this is just basically an api but they only miss uh the is very very similar to flash clicking flash we have at update route but in i have the dot app dot the the the type of method of that route right you get or post delete uh or something yeah so that's all now that we have that running let's go ahead and actually create another folder another thing right there we're going to call in models uh models.pipe so in models of paris we are going to make our databases or we are called we call them models or okay so we're going to say from from tortoise uh from total models you're going to be importing input you're going to be importing model and then we're also going to say uh from uh tortoise this actually from tortoise uh from tortoise uh going to also going to import i don't know it keeps giving me that import uh fields so let me fields yeah and then um and that's what we need for now so let's actually create a class and then it's going to be very similar to you can call it product so for products in here going to inherit from model right this is going to be a model so now that you have inherited from model you can write in the id and the id is going to be fills fills dot int and uh there's gonna be inked today autocomplete this is bugging me so there's gonna be uh input field and then it's gonna be a primary key so i'm gonna say pk is true so we're going to say the primary key is going to be two so the id is going to be our primary key and then because we fight the name and then the name is going to be uh fields uh fields dot uh this is gonna be a car feed a car field so it's gonna be a character field okay so this could be a uh field and then we can provide in the the other attributes like the max the max length can specify the max line to be let's say uh the product name let's say max length is 30 we can also provide in a nullable meaning that it cannot be uh nullible actually the spelling right so nullable is going to be uh false so now i was going to be false and then we're going to provide in the quantity the quantity uh quantity the sequence in stock and the quantity in stock we can provide into p uh it's going to be an entity right because it's going to store the quantity and then we're going to you're not going to allow this month because the quantity is actually a whole number right it's supposed to be a whole number you can say i have four point five groups just be like four or five right so it's also going to be using uh an integer so i hope you got that concept so fields dot uh int field and that's all we're going to say uh into in field and then we can give it a default so you can say default uh in case they didn't provide you're going to say default is 10 uh just in case uh sorry the default is zero rather so the default is zero yeah and then we can also providing the quantity uh the the quantity is also against a quantity quantity underscore sold so the quantity sold is going to be uh fields uh fields that uh the quantity sold is also going to be of type integer field right because uh yeah so you can say default in case yes we can say the default is zero but to be required to provide in the the quantity sold okay but you can keep it there a default and you can also say the unit the unit price so the unit price is going to be the price of of the product each unit price so it is it's going to be a a decimal field because we we can allow decimals so it will be here it's going to be a decimal field right so fields dot decimal decimal field and then this this month will be you need to provide in two things you can say max digits uh max digit you can i i mean that to be uh eight let's say eight and then number of decimal places we can say the decimal underscore uh places is going to be this one plus is going to be a 2 and then the default is going to be 0.00 and then in here is supposed to be number of digits and then this is supposed to be filled yeah and then uh i thought okay you're also providing this applied base so the supplied by is gonna be uh supplied but it's gonna be a foreign key of uh a field of another table that you're going to create so we're going to say uh fields that uh this is going to be a filtered forum foreign key field and then in here i need to provide in the the model which is going to reduce so i'm going to provide models which is the name of the file dot suppliers which is going to be a table that i'm going to create uh very soon so it's actually supplier singular uh follow that name and convention and then related we can write in the related dip and against the written name to be uh goods underscore supplied right so i can creating the related them to be goods uh supplied so now that we have that let's go and actually uh now put in the last video is gonna be revenue then the revenue is going to be fields dot uh fused uh this is gonna be uh revenue can be also a decimal field right so decimal field and in here i can provide in the max max digits i can play max series to be let's say 20 and then you can say decimal decimal decimal places this one places can be let's say three and then the default uh the defaults we can say is zero point zero zero and then zero point zero zero okay so uh in here i think that the decimal place will be also two because two two decimal place i don't think can exceed two right it should be uh two or under two so yeah then we can have we have all that faces so each product is going to have a name each product is going to have a quantity in stock each request of quantity sold that's actually quantity sold and then each product is going to have a unit price it's going to be of type decimal each product is going to be having a supply right so it can be supplied by someone sorry so let me just move the move the the foreign foreign field to be on its own here so you can see we have the revenue each project are going to have a revenue and it's going to be of type de small type and then you're going to have a product this product is going to be supplied by a supplier you can this is going to be a full a foreign key to a table that i'm going to create called uh supplier and we're going to put it in the details okay so i hope you have understood till there so let's go ahead and create the table for supplier let's call it supplier and then for supplier we're going to inherit from model and then um this is each supplier is going to have an id and the id is going to be uh fields that is going to be and in the data end field right because each primary is going to be of type integer you can play this a primary key is going to be true so we're going to apply the primary key to be true and each supplier is going to have a name and then the name is going to be fields dot uh dot co field so each is going to have a name and the car field and then you can provide a max length the max length can be let's say 20 so we're going to have a name of maximum of 20 characters and because by the company in which the player comes from so we can say company is uh the the fields fields that are carfield it's going to be a car feed as well and then the max length you can provide the max length to be let's say they know the company has a max length of 20 characters as well and then we can just provide in the email of the sub player so it's going to be email and it is going to be a fields uh that is going to be also a car field sorry character uh character field and then this we can also provide any other max length so make sure i get right so max underscore length that's actually th and then the max length is going to be let's say uh email let's say 100 i don't know we can also provide in here the phone number of the sub player which is going to be phone and then the fields uh fields that uh car field and then you can write in the max max length and then the max length can be um let's say 15 you can leave that to be 15 or yeah we can leave it to be 15 if you want yeah so that's the basically the sub players table so is going to have an id not uniquely against it identify the supplier it's the place going to have a name the company in uh from which where the supplier works the email of the supplier and then the phone number of the supplier okay so now that we have that you know we need to go ahead and actually create something on us identity models if you're not identities uh it's a library i think it's a library in python you can just google it is a very very easy thing to use with fast api and if any very intuitive to use rather than using uh my sql library sql cameo stuff something like that okay so uh let's go ahead and actually create it to spidenik so we're going to say uh from tortoise dot contrib uh dot pythentic pedentic you're going to import you need to import uh we're going to report by identity by identity model model creator okay great so now we're going to come down here and then you're going to uh create create a bidentic models so here so we're going to create a pyramidic models and i'm going to create the panetta pyramic models for the board products in the software so let's start with the products so we're going to call the product uh pedantic uh product by dentik and this is going to be a pedentic model creator you're going to pass in our model name so our model name is going to be group product uh i call it product it's just let's call it product okay so product and then uh product i'm going to say product and then the name of it i'm just going to then on the table i'm going to call it product called the same name to avoid confusion and then i'm going to create two paretic models one of them is going to be used to accept a incoming data so if you're going to accept the incoming data you don't need to accept the id and other fields that are only read only feels like the id field is only read only right so we're going to create a python model it is going to accept incoming data and the incoming data is not going to have uh it's not going to contain read only read only field so we're going to write another identity model and you're going to call it a product i just called pandatic in and then it's going to be it's going to be excluding the read-only fields because whenever you're creating a user right the id of the user is determined by the database right so you are not going to be expecting that from the database so this the id is only a read-only field so if you say pedentic uh we create a model again and then we say uh read exclude underscore read only equals to true this will exclude the read only models and we can use this model to be able to accept user inputs so i'm going to go and actually create it by identic and i'm going to say in i'm going to add in in there and i'm going to go by identity model creator and i'm going to go pass in product because i want to create it for the product table and i'm going to call it give it a different name okay if you don't give it the same name also you get an error so i'm going to call the product product in and then make sure that i i specify one more field exclude read only so exclude read only is going to exclude the fields that are only read only fields for example the ids explained earlier because if you are creating a user you don't expect them to pass in their id right so that's all what the read only field is going to do so now let's go ahead and actually create another one one more pedentic module so this is going to be for the sub player so it will supply underscore uh pedentic and it is going to be by identity model creator and i'm going to pass in here the sub player so i'm going to pass in the sub player class then the name i'm going to supply the name as well as supplier the name is going to be supplier and then we're going to do the same thing for the player we also need to uh have a model that excludes the red one if used for the player so suppliers right so i'm going to use a player under scope identity and this is going to be pedentic mode creator i'm going to pass in sub player in here so player and then the name is going to be uh supplier is going to be supplier in and then make sure the naming the names of the tables are different also get an issue okay so and then we're going to say exclude exclude underscore rate only and it's going to be true to exclude the read-only field so whenever you're trying to uh accept user's input and you're going to see this just in a moment for now it might not make clear sense to you but just wait for me and uh just like bear with me i'm going to explain this to you when we try to create our uh you to accept user input so this for now this is a basic concept of our database so now that we have that in the in place what we need to do right now is just to go back to our app and then in our app right we're going to go ahead and actually uh instead have write some code that will have this that to have that to have a models register whenever we try to execute our application so now you can see let me just open up this you can see i have no database right here i have no database file even if even if my application is running you can see i have no database uh in my in my application yet so if you have created a database let's go ahead and actually make sure the database is created so i'm going to go to up and then for me i'm going to go to uh i'm going to say uh import some libraries from tortoise dot contrib dot first api import uh register by the register totally so this is just a library that comes from total it's contribute.apa we're going to import register totally so what you deserve totally uh function allows us to do it's actually a function it allows us to register our models right so i'm going to come here at the very bottom of the app and i'm going to go register to this model and in here i'm going to provide him you can see texting up so i'm going to write in the name of my app which is the instantiation instantiation uh instantiation of the app so this app here is what i provide in here okay then i'm going to go to db uh url so db url is just a route to that database so i'm going to create an sqlite database so it's going to be an sq lesson with sql sqlite and then i'm going to go uh colon and then two false lashes to false lashes and then the name of database i'm going to call it database dot sqlite sql lite 3 okay i'm going to go database of sqlite3 and then in here i'm going to providing the modules so i'm going to say module modules the modules is going to be a dictionary so i'm going to write in dictionary and i'm going to go modules and then i'm going to go in here and type in modules so models is the name of our of our models file here model so actually going to be a list so i'm going to say lists models and then this model is gonna be so i'm going to go ahead and provide in one more argument is actually uh generate scheme generate schema and this is going to be true to only policy generate schema if we try to run our application and then we're going to say add as exception handler handlers and i'm going to say this is true to handle for us those exceptions so now after typing in that you can find this on the official uh flask uh documentation so just copy that and paste any in your application or just type along as i have did so this is going to be your app instantiation it's going to be there they wrote to your to your database and then the router database i mean this is going to be the models it's going to be the models and then we put in that model here i think it's the name of the file is going to be here and then if we wait in the general scheme is going to be true and then accept add exception handlers are going to be true now that you have done that you can go ahead and actually run check if application is running again so i'm going to close that close this sometimes it gets stuck soon just open up a new one so i'm just deactivate when i'm going to run the uv cone command it's going to start our development server so module fuels has no attribute curfew so you've already made an error so let's just go back and try to fix that error so the error is somewhere around um around uh let's see so fields fuel fields let me just try to highlight all the fields okay so it's actually the character field so it is in here so actually car field that's that's so uh fuels.carfield and then that's that so let's check again uh try to close this it got stuck again i don't know why it's freezes at times let me just try to refresh this it has frozen again so let me just well let me just create a new one i don't know if i just keep happening is going to press enter and then you can see uh it says that application of a logic slip try to exit this one close the terminal uh let me just clear that it also has frozen i really don't know why it keeps free freezing so it says that no module name supplier registered in app model so no model name supplier so let's see where is getting the error so and uh yeah so i have an error here so this actually a capital capital is supplier okay because this is the name of this class right here so this is the name of this class that you provide in here and i specified a small later rather than a capital later so let's go and actually make sure we uh repeat the process again in my case it freezes i don't know if the same thing happens to you but in case if it doesn't fringe uh doesn't freeze you don't have to repeat this process again okay so i'm going to go bin and then activate uh clear that and then i'm going to go uvicon uv cone and then up colon up and then hyphen hyphen reload so press enter and then you can see that it's running so you can go back to application try to refresh and you see everything works fine so now that we have our database set up we don't have any errors so in the next tutorial i'm going to go ahead and actually try to create the crowd functionality for the products and then the upcoming video create a credit functionality for the sub players and then finally create the email functionality and then move on to front end development so yeah now our model is set up and this is the model each for each uh product can have an id a name you can just a quantity in stock the quantity sold the unit price the revenue and then the supplied by the supply will supply that specific uh product also each product of a each supplier have an id uh that uniquely identifies them their name their company their email their phone number and yeah and you're simply going to actually create the pidentic models and then we came up here and then we registered our model our model suite tortoise by specifying these parameters so that's all and you also learned how to um how to return a simple json using uh using fast api and don't forget to don't forget the that this uh app.gate is actually a uh a decorator and not just uh update okay it's not like a class or something it's actually decorated if you're not about degraded impact don't have a whole series on decorators on my channel i'm going to leave a card up here you can check out that uh tutorial if you have a way to leave a card you can also find it on my real channel page you can just google to uh go to my playlist and you can find it there so guys thanks for watching thanks uh in the next row quite actually try to create the front credit functionality for the products so uh see you in the next tutorial keep safe
Info
Channel: Code With Prince
Views: 8,704
Rating: undefined out of 5
Keywords: python, pydantic, dataclasses, youngdevps, fastapi, api development, python web framework, create api python, tutorial, apprendre fastapi, learn fastapi, tortoise orm, tortoise, sqlite, api, programming, react full stack app, react, react for beginners
Id: tWv8Mi35jSQ
Channel Id: undefined
Length: 29min 4sec (1744 seconds)
Published: Wed May 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.