FastAPI Creating Models using Pydantic - Data Validation module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to be showing you guys how to use the pydantic module with respect to the fast api let's get this video started alright so here is what we left off in the previous video we saw how to create multiple routes and we also saw creating a generic route as well but for this video i'm going to be showing you guys how to accept a body request which in other words you can consider body request usually usually comes as part of a post request so how do you accept a body request in fast api so there's two reasons why this fast api is considered very very ultimately fast compared to any of the python frameworks even including flask and django so the reasons why this is super fast is because it uses pydantic as a datum validation module and starlet in the uh in the layer or in this example we are using ubicon as our base layer for creating asgi layer level of server so this is the two reasons why fast ap is super fast so what is pidentic here is that it's actually a data validation module all you need to do is just install it using pip itself just say pip install pydantic now like i told you in the first video everything that you see as part of your fast api creation is actually used uh when you are going to be needing it for on on demand basis it is not actually part this bite ending is not part of your uh the base module itself so like you can see here it's already saying that pidentic model is already installed in my system because i already did it so if you guys can do it as well it will just you can get started with this video all right so let's go to the top and first of all import from pedantic i'll import my base model so what does the base model really do now in this post example uh which we're going to be creating now in this post app method we're going to be creating i'll remove all the other app methods uh the delete as well as the api route as well all right so in this post example i want my server to accept a body request that is going to have different fields in it for example name age country and things like that i wanted to accept it as a body uh not as the header so in order to do that we are going to be creating a simple model uh which will automatically take care of all the orm related things meaning that it will automatically take care of data validation data visualization meaning it will create it as part of a redox and docs as well and what i'm going to be doing is that i'm going to be calling this as a name probably i'll call it as name country or name values something like this and i'll inherit the inherited from base model so what does the base model do is that any field that you're going to create inside it for example i'll say name and i'll say that name is going to be a string all right so this let's let's let me move on to the next one choose lowercase str right all right so it's i'm gonna be saying name followed by i will say country because country is also going to be in str and i'll say probably age and ages could should be an integer uh oh yeah i want it to be only an integer not a float and probably i'll say base salary right base salary uh is going to be in float so what i'm exactly doing here i'm saying uh create a field with name uh accept a field with country accept field with age and accept a field base salary so all of these things are by automatically as soon as you inherit your base model and create your data fields automatically this will be taken care of this will be the the variables or this will be the only allowed keys that your method can now accept now how do you do that now just say name values right i'll go here and inside this i'll say i'll remove the username i'll tell now accept my name value and it's going to be of type name value now this uh the the fast api is now much more capable of understanding there should be a name value that should be accepted now and this name value should be of type name values which is going to be a base model that we created now all i'm going to just do now is that i'll just say probably uh print the name value just for the example sake right and i'll return from the you from the name value i'm going to be now returning probably i'll just return the name back so the name i'll say name value dot that's it my work is done here so it'll automatically now return the name value and will automatically considered it as a tuple as well so we don't have to worry about it and what is happening now is that we can now send a body request instead of header request you can now send a body request with all of these data values present now let's run our server to see what really happens uh the commander on this is to basically say uh i just cleared okay uv con i'm gonna say ubcon and app main sorry main app i keep messing this up fired up let's let's go here and now i need to do is i just need to go to uh docs and as soon as i go to dogs it will automatically have my post data present now if you go here and if you go here you can automatically see that my uh name value which uh inherited with base model from the pythagorean is now automatically giving me a visualization meaning i can now see for myself that it can accept a string countries string agency integer base salary is a float now all of this is accepted now i can test this out by just saying yeah now i cannot test this out by just passing the values here i can now say name is going to be watch and the country is going to be india age is going to be 24 and the salary probably i'll say that it's about 1500 point 10 one something like this all right that's i'll just say thousand five hundred dollars and i'll execute this and you can see for yourself that the name bharatwaj is written meaning i it is actually now able to take care of it now let's now do something else now instead of 24 here i'm gonna say uh probably um chennai where i live it's not an integer right now if you execute it you can see that for yourself it throws a 422 and processable entry and back here it says very clearly why that thing is not accepted it says that value dot error jquery i mean value error when i j when it is trying to json decode and it's also very clearly saying that i need to pass is expecting a value and instead of that i'm getting something else so this thing is automatically taken care of by fast api we don't have to worry about creating a clean uh error response code and that is all over red when you're trying to use flask or django you need to every time create our probably we need to define what is the error codes we need to say what should the user understand from the error codes all of those layers are taken care by fast api and it's amazingly cool all right so now this is uh interesting it says that every field is now required every field is now required right but if you want to say let's say i want to i don't want few fields i can just now directly say just initialize it to none and will automatically not make it default it will make it will have a default value of none and not make it as required and that's pretty much how you create your you can send post request with the help of your pideantic module and only if you use the identity model you can send a post request via the body meaning if you don't have the body you can still send it via the header and i like it or in the previous video it's not recommended alright so is this the only way to now enter body values meaning that this is the only way or if you only use your pedantic can you send body request absolutely not you can still send body requests by importing the body from your fast api's base module itself you can just say uh import my body which is a which is actually a class that is automatically going to recognize your methods and create it as a body method now let's say i want to uh add a new parameter here like spousal status or something like that i'll just say spousal status and i'll probably want it to be a string married and married probably that's the two values that you want and now i'm gonna be saying that instead of uh taking it as a header if you directly put it like this it'll automatically be considered as a header but i don't want it i it to come as part of a body so what i'll do is i'll just say body off and i'll just make it default i take everything as default now one more thing here is that this should not come in the first it should be in the end meaning that the parameter placing is very very important so let me put it in the end or the last of my thing all right so now the spousal state is considered as part of the body itself and now i can now send it back again you can see for yourself uh that it will actually be used and it will probably come as part of our talks and redoc as well i'll just say spousal status right and now let me fire this up again and i'll show you guys how this thing whole thing works all right let's let's uh fire it up and just as you can see this whole thing is coming as part of a dictionary and the final thing is coming as part of a string now one more thing that you also can understand from this is that if you try it out uh you can see that this is going to be bharat what's my name and my country is going to be india i don't forget about this age is going to be 24 probably and the salary is going to be 1500 bucks all right and the spousal status is going to be unmarried right something like this and when executed it will automatically take care of doing it for me and sending it back right at me so i don't have to worry about adding new values i don't have to worry about uh giving a new key to my body and all of this is automatically taken care of by fast api now this is uh for the docs and i like i showed in the first video we can still do the same with the redox as well and this is also going to be having the same steps and it will it will be very very easy for us to continue uh when we are actually creating these apis so pretty much that's what i wanted to show you guys for this video hope this video is informative and if you have any other questions let me know in the comment section below uh let me meet you guys in the next video we have uh videos coming out with authorization security how do you authorize your fast api request and all of those things are coming up right in the next videos i'll see you guys there uncleans bharath peace out have a surprise
Info
Channel: CoOoDE
Views: 10,743
Rating: undefined out of 5
Keywords: coder monk, programmer, software development tutorials, coding tutorials, fastapi tutorial, fastapi python tutorial, fastapi python, fastapi tutorial for beginners, fastapi tutorial python, fastapi pydantic settings, fastapi pydantic validation error, fastapi pydantic orm, pydantic tutorial, pydantic fastapi, pydantic basemodel, codermonk, python coder monk
Id: GkrDmUEEEtM
Channel Id: undefined
Length: 10min 11sec (611 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.