Fast API Tutorial, Part 11: Extra Data Types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome back to part 11 of our fast api tutorial um this part and the next couple of parts are going to be a little bit shorter um just because it's there's not a lot of information but i want to keep it you know like the offspring i want to keep it separated okay i think it's the offspring i might be dating myself there but whatever that's fine okay so let's get right into it now you can see here we've got part 11 is is highlighted everything else is uh is commented out our app is up and running and we are good to go now before we get into actual any actual code um i'm just going to remind you that this this fast api sort of stuff should work with any of these not any but it should work with most of these field types that you see we've primarily only been working with primitive types like integers floats strings booleans things like that but you should be able to i think we did one that was a a url i think but you can see we've got there's date time there's um you can do unions of things you can do sets um you know the the world is your oyster if you will we can look at enums and choices we kind of already did this a little bit using base model um fidantic types file path color like all of these things are available so i encourage you to go ahead and play with it um one thing that i am going to uh point out is this uuid uh it stands for a universally unique identifier um and let's let's actually take this the uuid and let's take some date types and let's just build a route and let's see what we can do with it okay so let's set up app.put items item id and we will call this read items now what we're going to put in here item id instead of it being a string or an integer we're now going to call it a uuid type and in order to do this we need to import from uuid import uuid and we will see what that looks like if you've not seen that before don't worry we'll see it in a few minutes let's add in a couple other things here let's add in a start date which will be a date time or none which will be a none and let's just so we're gonna have to import this as well from date time import date time and i just want to show you this right here so let's return item id is item id and start date is start date now something is not going to work here and i'm going to give you a couple of seconds to try and figure out what's going to be weird in this situation so what type of parameter is this going to be that's kind of what i mean but it's not going to work like we we want this to be a body parameter and i'm giving you a hint that it's not going to work and the reason for that is because we've not actually this is as far as we're concerned just a regular type it's a built-in type we've not set a a model using base model we've not done any of that so we're going to come in here and this is going to be a query parameter this date time object is going to be a query and sometimes you want that um i'm building an app right now that takes in a date as a as a parameter because that's what we're filtering on i don't want to expose ids so i'm exposing a date so we can do that but here we want it to be a body object so what we're going to do is we're just going to say body of none because we don't care about any other information this is just a small example now what we're going to be doing is we would be passing in an actual date time object let's add in a couple more things end date is going to be date time or none which will again be a body object let's save and refresh and now it should be a dictionary there we go let's add a couple more items here um repeat at will be a time object and then when this isn't going to do anything we're just going to actually return what we're passing in but i just kind of want to show you some of the other options for what you can what you can pass in here and let's do a process after and let's make this a time delta or none there and what we will do is we will call something else start process equals start date plus process after and then duration equals end date minus start process so you know i don't know what this would be for you know you've got something that you want to um you know you you want to have it repeat after a certain amount of time i i don't exactly know why you would do this but this is just a very simple a very simple example that uses time time delta and day time which we the latter two of which we need to import so there we now have time time delta date time now let us set actually let's just return stuff now i don't want to do any more of this and date is end date um repeat at is going to be repeat at process after will be process after there's got to be a better way to do this but i don't even care right now start process is start process and duration is duration save let's refresh now we can't use our trusty little one two three that we've used before it's not going to work because this is not an integer we can't say hello because we need to actually pass in a uuid type for those of you who are unfamiliar with it let's just open up our terminal right here you can see i've got one right here but let's quit and just kind of let's do it just from scratch open up a python instance and we need to import uuid or you can do from uuid import uid4 and then all we need to do is uid for and execute now we copy this entire stuff in between the single quotes we hit that and now we're going to get an unprocessable entity i always want to say identity entity but don't worry about that what we can see here is this worked this uuid worked the unprocessable entity has to do with this repeat at value it can't actually convert the word string to a time you can put in a number and it should work you can see this is now going to repeat at two seconds midnight plus two seconds you can do um 16 30 let's see if that works there we go 16 30 which is 4 30 pm for those of us in the u.s and 26 seconds okay process after can be um let's say thirty six hundred i mean it's just it's just a number so so there we go and we can see that uh that the start date plus process after gives us a star process duration is end date minus start process we get these values here so that that's kind of it for this video it's not going to like i said it's not very long it's just you know to show you you can use more than just your standard primitive types again if you this is where i would encourage you we talked about uh including regex in some of this before for validation purposes i would encourage you to look at the fidantic docs to see if there's a field type that already conforms to what you need to do because you know trying to trying to implement regex like we saw when i was trying to do it i just i failed miserably okay in the next video which is also going to be short we are going to cover cookie parameters and header parameters i'm going to kind of squeeze those two into one video because otherwise the video on cookie parameters would be all of i don't know 30 seconds it's really not going to be very long okay i will see in that next video
Info
Channel: JVP Design
Views: 5,100
Rating: undefined out of 5
Keywords:
Id: -Ncz4Vi5F88
Channel Id: undefined
Length: 9min 45sec (585 seconds)
Published: Tue May 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.