E-commerce API with Fastapi | Database design | Tortoise ORM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello youtube welcome back to the second tutorial in our e-commerce uh with first api series so in this draw we're just going to go ahead and actually begin to to build our models okay our database basically so let's get started so i'm just going to bring up my terminal and i'm going to create a file i'm going to create a folder rather and i'm going to call it a commas e-commerce underscore api just something like that i'm just going to create it and i'm going to change my directory into that e-commerce api okay so now that we have that we need i'm going to create a python virtual environment so for now i'm just going to deactivate my conda environment so if you don't have corner then you don't you don't have to do that step okay so now that you have the let's go and actually create a python uh virtual environment now this is going to work for what i'm going to type in is going to work for linux and mac os so if you are on windows there's a special command that you need to google how to create a virtual environment in google in windows so you just google that and then you can get the instructions to do that so go ahead and actually google it if you are using windows but if you're using mac or linux feel free to follow along so it's going to create a virtual environment and i'm going to vmv and i'm going to call it vmv and then simply press enter this screen takes some time and then it's going to create for us the python virtual environment so if i do ls you can see i have a folder in there called venv which is the name that i specified here okay so this other command strip must be one to one but this one you can specify any name you want okay so i just call it my v and v and increase the for that called v and v where our virtual environment is so let's now activate our virtual environment again for windows users you need to google the code to activate your virtual environment but from linux and mac macos users feel free to follow along so this is going to do source source vnv ve nv for slash uh for slash bin for slash activate okay you're going to activate that virtual environment now the virtual environment is activated we can see the name of the virtual environment within parenthesis at the beginning of our line so now that's done let's go ahead and actually install the libraries that we need okay so i'm just going to go to google and in here i'm going to type search for uh first api just search for that uh first api and then search for that so this is going to bring us the first a first api official documentation and this is just the framework that you're going to be using to build our application so you can see first api is currently a very new framework in python uh and uh it's quite good it's really really good i like it and it's faster than the advantage of fast apis that is faster than django and flask since it's up uh it's supposed to have sync i operations which uh flask and flask has recently introduced but i don't know if chang was in implemented that so guys let's get started copy this and then let's install first api so i'm just going to go back to my terminal and then paste that there pip install first api or you can also do a f3 install first api okay it's going to install first api within our our current virtual environment okay so it's going to take some time and then it's going to install for us that first api so great so now the first api is installed we also need to install one more thing called uv con so uh if let me just scroll down maybe it's here under the official documentation so again also yeah this is a uv gun so i'm just going to copy this as well you also need to install the uv cone now what the uv gun does is that it helps us to run a server okay so it helped us to run the server and like in flask django we have the development server now to run uh the app in first api we're going to using the uv con library okay so now that we have that now let's go ahead and actually install one more library called total user rm so totals rm is a database that you're going to be using so let me just uh search for it on google uh toys or rm and then we can just search that on google and then we can get back the results for uh total ram and then we can just go to installation uh let's go to getting started and if you're going to get inside this uh how to install it so we're just going to copy that and then simply paste that in our terminal and then install that library which is sp pip installed uh total srm because unique also do peep three install uh tortoise orem so feel free to use the one that you are comfortable working with so great so now that you have that installed i can just open up my visual studio code and then we can begin uh writing our app okay so i thought we're going to be creating um we're going to be creating we're going to be creating the app here so let's just create in here uh a folder right you're going to call this i'm sorry a python file i'm going to call it model.pi uh this we are going to keeping a model so let's um just call it models it makes more sense if i call it models so let me just rename that to be uh models okay so i have that file called models.pi and in modulus of spiders we're going to be writing our model so uh we need to import a couple of things in order to get started so i'm just going to bring it you're going to import once this is from total srm so we're going to go uh just close this from our tortoise and import uh model okay so we're just going to use it from a pedantic uh import uh base model okay so for now let's just import those two libraries and then we can go ahead and actually begin to create our database so i'm going to create the two databases so actually three databases one for the user one for the business and then one for the products so let's go ahead and actually create the one for the user so class user and this is going to take in model you're going to inherit from the model that you have imported from uh tortoise or orem serving to go id each product each user is going to have an id and this is going to be uh fields you also need to import these fields from total srm so i'm just coming and i'm going to import uh fields okay so i'm going to just come here and then say fields dot uh is gonna be int field an integer field and then it's going to be a primary key you're going to set this to be a primary key so then set that to true and then index you can index that to be true okay because you're going to be using that to create our database also needs a username and then the username is going to be fields fields dot uh character field so it's going to be a character field a character fields uh character field and then this is going to be uh have max length you can specify the max length of this we can leave the max line to be 20 the username we don't want anything more than 20. now it's going to be false we don't want it to be empty means that each user must have a username and then we want the unique uh unique to be true unique should be true because we want the usernames to be unique okay and then you're going to email and then the email is just going to be uh fields.uh carfield and then the car field is just going to allow us to have some some email email in form of text so we're going to have maximum length is to be 200 and then you're going to be now uh each year must have an email so now it's going to be false and then you're going to have a unique unique is going to be true so sorry unique is going to be true and then you're going to have a password so password each user will have a password and then the password is just going to be uh fields dot carfield right a character field we are going to be storing the user's weight sorry i don't know why this keeps happening uh car field and then this is going to be having a max length a max length is going to be a max length of 100 and then now uh now uh now be false meaning that you must have a password right and then it's going to say we're going to have this is a verified and it is verified is this going to be uh fields dot uh which is gonna be boolean field uh fields dot uh boolean so uh boolean field uh so the boolean field is just going to be a true or false by default you act all accounts are not activated right and then you're going to also have join date underscore date and then you're going to have uh fields that this is going to be date date time field and in the days time field is going to have a default a default it goes to uh time date time dot utc now i'm going to input daytime from here so i'm going to say uh from date time import click it import date time it's like that's from daytime we're going to importing date time so we're going to move down here i'm going to say date uh daystime.um utc now okay so this is just you passing that you don't call the function you just pass it in so it's going to be created using this going to be calling it in the background you don't specify the parameters here you just pass that in there so join date is going to be fills dot dates time field and then default is going to be a utc now okay now let's create the database for the business okay so business and then it's going to inherit from the model that model that you have just imported from uh total swarm so this is going to be uh this is going to be fields dot uh integer an integer field and then this field is going to have a price going to be primary key for this table so primary key is going to say that to be true i can set index to be uh true so if you can be use this field to filter okay filter our database so business underscore name and then this business is going to have a name so it's going to be fields that this might be car field a character field in this graph the field can have max length then the max max underscore length the max length can be let's say 20 and then it must not be now so now is uh false and then uh this business uh name right nullable okay so this business name has have unique equals to uh true each business name must be unique great so we're going to have city it's going to be fields that uh a character field and then this is going to have a max underscore length and then the max length is just going to be 100 and then we can just specify null now to be false and then we can also specify default case default and default to be unspecified okay in case they didn't specify so at the point of registration i won't give them the ability type in their city they'll just register because they'll just register when when it just registers a user then the business i'll be created for them out the business model that the account in the business database right will be created for them but the default field will be unspecified and then later on they can have an update functionality in case they want to update their city okay region and then the region where the businesses we can specify uh fields fields dot uh car field and then the character field is going to have a max length then the max length is going to be let's say 100 and then the is now cannot be now so this is not going to be false and then you can specify default and then default can be also unspecified great so now that you have that you can also specify the business uh it's gonna be the business uh description maybe a little bit description about the business uh what they do and what the services that they offer so build uh it's gonna be fields dot uh text text field so there'll be a text field because it's going to be a bit of a text so uh now now you can say now it's going to be uh false this right now can be true it can be empty because if you don't provide a description that's also fine and then you also have a logo each business will have a logo and this would be a fields dot um this would be a character field a character field and in this cluster field it can have a max length and the max length can be let's say 200 yeah fine then we can also specify uh the null now can be uh false and then you specify the default then the default is just going to be a default dot j jpg okay this is going to be a default image that we can keep in our file later on where the each business that doesn't have its own logo will default to the default logo right so owner is just going to be a foreign key so it's going to be fields fields dot uh foreign foreign key foreign key field and then in unit provide uh which which model is going to be a foreign key so we're going to say models dot user okay you're actually referring to our file which is called models and then you're referring to this class of it okay so referring to a class called models sorry our python file called models and then we're referring to this class inside of this file right so that's where our foreign key is means that each business will have an owner attached to it so related created name you can specify the reader name to be um business just like that that's all we need for the business uh business uh database so let's create the one for for the products we're going to call it product and then it's going to inherit from model as usual and then it's going to have an id this is going to be a fields dot intege into int field uh infield language before the primary key is going to be true you can say index you can say that to be true and then we can use the name each each product is going to have a name and then the name is going to be uh fields dot um character field and then the character field is going to max length is going to have a max length let's say 100 now it can be a false and then we can specify uh index the index can also be uh you can just make use this as an index first you can use the name to filter that so each product is going to have a category uh so again this just be a fields dot um character field in this character field is going to have a max length in the max length let's say 100 or let's say 20 or 30 that is fine and then we can specify uh the index index can be true and then we can satisfy the original underscore price and then the original price is going to be uh fields dot uh this is going to be a decimal decimal there's more field we're also going to be storing in their uh decimal value so decimal field and max max dig it you can specify max digit to be uh max we can be 12 and then we can specify the decimal places decimal places can be two uh yeah that's what we need so and then we can also specify the new price new underscore price so new price of the product will be the the the updated price in case that the user wants to have a discount field right for example the user maybe would want to we want to maybe had a product original price was 100 and now he wants to save that a discount of 90 so this would be the filter to store that discounted value and then we use that to also calculate the percentage of the percentage discount of the product so we're going to call max digits max degrees again can be 12 and then decimal places decimal places can be 2 again so because this also percentage percentage percentage underscore discount so this we're going to calculate the discount so this is going to be it's going to be an integer field so uh this is going to be uh fields dot decimal it's actually a capital d decimal uh field just like that and then sorry uh this wasn't this console is rather inch field so it ain't a field just like that and then we can also go over offer offer uh expiration so expiration date okay this one this is when the offer or the discounted is going to the discount is going to be expiring so this is going to be date date time field and then in this desktop field they're going to add a default so this is actually just going to be a date field not a date time field so it's going to be a date field and then in the data field you're going to have a default and the default is just going to be data [Music] data date this actually date time date time uh date time dot utc uh now that's all don't call it just pass it in so this actually feels uh feels dot date time the date field uh and then the utc now okay default is utc now and then you have product image right so the product image is where the image of the product is going to be and the product image is going to be again another field of fields and it's going to be a product going to be a character field basically it's going to store the the the name of the image right so max length you can specify max underscore length let's say max length is uh let's say it's 200 i don't know 20 is fine let me say now it goes to false and then we can specify the default the default can be uh the default product image can be a product uh default.jpg you can call it anything you want so just call it product default default dot j jp dot jpg okay so we're going to have a default in case that they didn't specify a product name you're going to have a product a default product name called uh product default.jp jpg okay so now that's done let's also create another uh another foreign key here so we're going to call it business this is going to be business underscore uh fields then the business field is going to be a foreign uh foreign it's going to be a foreign uh a foreign key field so if we're in a key uh field and then this is going to have a the model that is going to represent the models which are paid and filed and then it's going to relate to the business right each product is going to be linked to this business in which it belongs to so uh then you have related related name you can call it product so if you get a business and we do business with products it's going to then trust all the products that's what the related name means so great now that we have those database created let's go ahead and actually do the so this actually dates time so let's actually go ahead and actually implement the identity created by datic uh identity models of these databases okay that's how things work in first api so let's go ahead and actually create user uh user underscore pedentic uh identities will be identic uh okay we need to actually import this pedentic so let me see so this is spider it's going to be pedentic uh okay let me check uh what you need so you need to import from total israel you need to import a certain library so again say from choice dot contrib that uh this is going to be identic uh we're going to be importing by then pedantic underscore model underscore uh creator so it's going to be used to create a pedentic model so let's go ahead and actually use it here so from uh from totally worm dot contribute pedentic import the pidentic model creator so down you're going to use the pedentic model creator what you're simply going to say is this will allow us to create a pedentic model so we need to provide in there the data that class in which you want to create a pedentic model for again the name is going to be uh you can call it user right user this is the name of the table and then you can exclude exclude we're going to exclude some fields here and then i'm going to exclude uh is underscore verified so the reason i'm excluding this field is that you're going to be using this field to accept the user's data or when the user wants to register now what we don't want to provide the user with verified right because they can be able to uh the front it can be able to just set the verified to be true and then this will be very verified we don't want that we want the user to be verified through the email account that's why we're excluding this again this is a tuple so don't forget that so great now that you have that degree going like to say user uh underscore identic uh pedantic in okay so this is going to be used to obtain the user's data from the front end so it is going to be by pedantic in and then this is going to be a pythag pedentic underscore model creator and then you're providing the model which is going to be a uh sorry it's clicking us to reset our user uh and then you can specify the name the name is going to be uh user in again and then we can specify exclude exclude exclude read only and then we can specify this to be true so we're going to exclude the read only fields because the we don't want the users to be able to update a read-only field so you're going to exclude them okay so if you don't know more about your total israel you can check out their documentation it's a very uh good uh library okay so you can just check out the official documentation it explains things in detail this tutorial i don't want us to okay i made a mistake here this is not a tutorial about uh totalizarm but since you're using it i'm just going to explain to you the basics concept so if you are interested in going deeper into the total srm they have a great documentation you can just google that out so it is creating another user pedentic out this is what they're going to do to return responses to the user we're going to call it user out and then uh we need now to specify x group exclude uh exclude you're going to exclude the setting field so i'm going to exclude the password field you're going to explore the password field uh just like that only if you're going to exclude whenever sending something back to send the user's information back to the user we need to be able to exclude the password field because throughout anyone to have access to a password even though the password are going to hash it in there later on in the series but you don't want anyone to have access to it so if you're sending anything back to the user you want the user's password hashed okay and not to be not to be revealed okay you're going to exclude that field it's very important that you exclude uh password fields because uh you don't want it getting to people right so identic so this was called business underscore pedentic and it's going to be pedentic model creator and you're going to create the pedentic model for our business class so business and then the name classify the name to be a business and then we can also specify uh we can also yeah that's what we need to specify for this and then it goes down here you also need to specify uh business pedentic it's gonna be in and then it's going to be pedantic underscore model creator we're going to pass in our business and then we specify the name the name or the name is going to be business in okay okay business in and then we can specify uh exclude exclude read only field and then the read-only field is just going to be uh excluded only if this is going to be true right set that to be true that's what we need and now we're going to go and actually create the last uh the last um the last the last identity model which is going to be the products right so product uh the scope identity this is going to be pedentic pedentic underscore model creator i'm going to pass in in here product so then we have product table and then the name is going to be a product yeah that's what we need for this and then we can go and actually create the product pedentic in so the products pedentic in so the product periodic in whenever you see a miniature grid is what you're going to be using to take data from the user so then just get this right this is the agreement we're using to take data from the users from the front and now we're going to exclude the read-only fields because the read-only fields that is going to be read only right so whenever the user is going to post a form or something to us we don't want them to post that back for example um it's verified right it's verified from there from the user's account it's only a read it read only field right we don't want the read only if you're the read-only fields can be like this foreign keys right here we don't want to send the phone case back to the you we don't want the user to send the foreign key to us whenever he's making a request right because there's no other foreign keys that's where excluding those read-only fields so for example i want to register i want to add a product right this product field expects a foreign key called a business model right now you don't want to do the user does not know uh the the foreign key the valid the value for this field right so we want to say exclude read only so the user is not expected to send it back okay so for example there are foreign keys right we just want you to give us a product name and the product details will deal with their foreign key ourselves that's what we're excluding their read-only fields and an example of a read-only field is just the the the foreign key fields okay great so i hope you understand we are doing that so you're going to be using this tool to obtain the result from the user and you're going to be using these to send results to the user okay so product and then we can have a name here the name it's just going to be a product in just like that and then uh in here going to have a we're going to have exclude we're going to exclude a certain field so this is going to be a tuple you're going to be excluding percentage percentage underscore discount okay we're going to excluding percentage discount you're going to also going to be excluding the product id okay great so that's we're going to be excluding so a person is making a request to make posting adding a new uh product to our database we don't want him to pass in the personal discount because this will be calculated and then we also don't want him to pass in the product id because uh we'll be able to figure that ourselves so that's all we need to create our database so great i'm just bring back my side panel and i'm going to have okay i think i just need to delete this okay machine deleting the right one okay so we have this one here so i'm going to create another file here i'm going to call it main dot pi so in main dot py this is what we're going to simply do is that create a simple uh first api app and then you're going to so i'm going to say uh from first api you're going to import first api and then you're going to be up it's going to be a fast api that's all we need to do to create that app and then we can instantiate it right there so i'm just going to register my models down here so at the bottom i can register my models and then i just um what i need to do is just to import certain things from totals orange so from totto is oram you're going to be importing something called uh register model okay so you're going to say up here at the top you're going to say uh from tortoiseoram dot contrib dot first api import uh register tortoise or tortoise okay so we also need to import our model so uh from models uh import i'm just going to import everything we're going to use everything from there okay so i'm going to register doctor is model and then any i need i need to what i need to do i need to provide in some certain arguments here so the first argument is going to be our app which is just the instantiation of the flask app and then db url the db euro is just going to be where the database is going to be so it's going to be an sqlite sql like database and then colon to for slashes data base.sql sqlite today certainly be an sqlite 3d database and it's going to be stored at a in a file called database okay simple and then you're going to have models uh the models is going to be a dictionary and then the first one is going to be models and then you're going to call the pass in a list and you're going to pass in models okay great and now you're going to get generated generated schemas and then this is going to be generate schema this will be true and then you're going to also pass in add exception handle uh hell uh handlers and this is also going to be true so that's all we need to register our ap our identity model so let's just create a simple route here i'm going to go uh uh are update gates and this is going to be uh just going to pass in the basic uh the just the index we're going to f index and then it should be you're not going to pass any parameters what you're simply going to do is going to return a dictionary okay so deletion you have a message so this traditional is going to have a message and then the message is going to be uh hello uh world just like that so if i save this and then we can go back to our terminal and then try to run our application so and make sure i'm in the directory where the application is so to run you just have to say uvicon which is the library install uh the app the file containing your your first api instantiation so you can see you know main dots by we have the first api ins instance instantiation right so this is the name that you're supposed to provide in here and then you need to say app which is the instances that you the name of the instance of the flask first api called hours app okay so now that we have the link to save the stash uh reload so you can also find all of these commands on the official documentation so we have a model uh we have an an error here so let's go ahead and actually fix that error so let me see which line is that so it's on line nine so let's go back in here i made a typo on line nine so here it's actually called interference in not that thing that i typed so let me just save that and go back to the terminal and then try to run it again so model totals field has no attribute infield okay this is online 18. okay i made a couple of errors so it's actually interviewed okay so i hope i don't have any more errors just say this try to run again okay good unexpected argument models okay so let me just uh excuse me it's actually called uh models and then this is models uh modules so this is actually not models it's actually modules okay so save that so the modules is just going to be that value right there so let me try it now okay now our app is running fine so let me just i'll stop this for now okay it has refused to stop so let me i don't know why it always crashes on me so let me just try to open it up again so i'm just deactivate my content environment and then make sure i'm in the source source so i'm going to activate my virtual environment and then i'm just zoom in so that you guys can see so what is i'm going to do in here just going to activate our uh run application again so i'm just going to close this window it always crashes sometimes i don't know why this always happens on my other laptop it doesn't happen but on this laptop it always crashes i don't know why so let me show that i'm in my directory and i'm just going to go uh uv gun and it's going to be main app and then slash put the reload flag reload okay then try to run that so you can see it's running on localhost output eight thousand so let me just go and try to open localhost at eight thousand and then this should bring that chrome and then let's see what we get so we can see we get back here hello world that's just simply it so in fast api it also comes advantage of fast ebay also comes an automatic uh automatically generated documentation so you can still have this documentation right here uh off right off the box we don't have to do anything for this and is simply going to return to us uh the dictionary hello world or json okay so that's guys that's how we are able to register our models and all our modules are ready so next row begin to register users into our first api uh application so guys thanks for watching if you enjoyed this transfer give it a thumbs up and subscribe to my youtube channel for more videos like this thanks for watching keep safe see you in the next one
Info
Channel: Code With Prince
Views: 12,851
Rating: undefined out of 5
Keywords: fastapi python tutorial, fastapi tutorial, fastapi authentication, python fastapi vs flask, python fastapi example, python fastapi swagger, python fastapi session, fastapi coursera, fastapi crash course, fastapi full course, fastapi create project, python fastapi course, fastapi, api development, python, python web framework, create api python, tutorial, ecommerce project, ecommerse project python, tortoise ORM, tortoise orm fastapi
Id: Mt6VKXRR_iE
Channel Id: undefined
Length: 33min 48sec (2028 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.