Fast API Tutorial, Part 21: JSON Compatible Encoder and Body Updates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome back to um our fast api tutorial this is video 21 where we will touch on the json-able encoder and uh updating databases using a request body if that makes sense probably should have written a script for that but whatever we're moving on okay so the very first thing that we're going to look at is just handling data and converting it into json so the first thing that we're going to do we're going to set up a fake db excuse me and then we're going to set up an item class like we've been doing a lot recently so we're going to set this up with title being a string time stamp being a date time and description being an optional string which we'll set to none then we're going to set up a put route app.put items id and we're going to set it up as update item id as a string and item is an item and let's see let's do fake db id equals item and then we're just going to look at our fake db and return success just for fun okay let's take a look and see what happens so we go here and we handle this we say hello for our id and we're gonna see that we get success as a response but this is an interesting little tidbit right here so you can see we have a timestamp we have an actual datetime object now when you're writing this to the database you can convert it you can do whatever you want with it but there are going to be certain situations where you're going to have a a database that maybe not a date time maybe a different data type but you might have a data type that's not compatible with your database so what you're going to need to do is somehow convert each individual field now instead of manually going in and do it what we can do is we can instead set json compatible item data equals json-able encoder and we have already imported this from a prior tutorial i don't remember which one but we're going to set that for item and now we will make this json compatible item data now i'm going to bring this up just a little bit so we can see the difference we execute and now we look we can see title is still a string but we get it's no longer an item type we get the timestamp is a stringified format we get the description as a string like everything is is good we don't have to worry about converting anything okay so this is this is the sort of thing that you're going to want to have to to handle when you you know you might not excuse me if you don't want to have to go in and manually convert everything this provides a nice little shortcut option for you okay now let's go back in we're gonna get into we're gonna update our item model so we're gonna say name is a string or none equals none i'll pull in the description from down here we will set price is float or none equals none tax float equals 10.5 and tags list of strings equals an empty list now what we're going to do is we're going to set items to be a small little database foo name is going to be foo price is going to be 50.2 let's set one up for bar name bar description is going to be the bartenders price we'll say it's 62 and tax is going to be 20.2 okay and then let's do one more just for fun let's do baz boss baz i'm not sure which baz description is going to be none price is going to be 50.2 tax is going to be 10.5 and let's see what are we going to do for tags we'll do an empty list of tags excuse me okay now let's first things first app.get items item id and we'll say the response model is going to be an item type just for you know making it look better in the documentation async def read item item id as a string and return items dot get item id now i'm doing it this way just in case i pass in something that's not a string and i mean honestly if we wanted to we could we should make this an enum so that it's only the items here the the keys here i should say but doesn't really matter right now let's update our put route uh update item we'll call this item id which will be a string item is still an item and let's do update item encoded equals jsonable encoder of the item items item id equals update item encoded return update item encoded okay we've got this all set up now let's go ahead and refresh our our page right here if we try blah this should not work well i mean it should return nothing but if we return if we fetch on bar we're going to get bar and we're good okay now what we're going to do though we're going to update this item right here so let's update bar now we're going to change this to bars we're going to make the description uh no i think it has to be null we want it to be a none effectively and we're going to make this price of 3. now a put request should replace everything so you notice up here for bar let's see if i can there we go so for bar we have a price of 62 and this tax here so what should happen is that tax is going to change because we're not passing it in here we hit execute and we notice the tax did change it went to 10.5 now this went to 10.5 because this is the default value here and we have the tags is a an empty list here effectively okay so if we were to pass in um you know if we were to uh tax 123 you can see the tax updates the tags still serve the default value and that's that's all fine and good that's kind of anticipated behavior okay now what we're going to do we're going to we're going to add a patch request app.patch items item id and here and here let's just do response model equals item i forgot to do that before uh def patch item and we'll say item id as a string item is an item now a patch request as opposed to a put should not replace all the data it should just update the data that we that we pass in so what we're going to do here we're going to say stored item data equals items dot get item id and then we're going to say if stored item data is not none because we need to check actually i think we don't even need this let's leave this out for now stored item model equals item stored item data we're going to spread out the key value pairs of the item that we're fetching from the database and we're just going to create a model because what we're going to do is we're going to use the update method that pedantic gives us or fast api i don't remember which one now update data equals item.dict and then updated item equals stored item model dot copy update equals update data and then we'll say items item id equals json able encoder updated item and we are going to return our updated item and let's also print items just so that we can see what it looks like now let's go ahead and refresh the page we're in our patch we're going to let's check blah and see what happens execute yeah there we go we do need that list we do need that in there um if uh stored item data is not none else equals item we're just going to instantiate a new item right there oh no store item model so let's just check this we want to have some decent error handling and error handling in here let's check out blah let's see if it works there we go we got all the default values okay so now what we're going to do is we're going to look at so we can see a set blah which is what we anticipated but now let's look at bar so we're going to take this again bars we're going to make this null and we'll make this three again and let's see what happens we see this we actually got 10.5 and we got the tags so bar if we recall correctly should have a price of 62 which we're overwriting with three but it should have a tax of 20.2 and instead what's happening is actually you know what let me instead of printing all of the items let's print items item id just so it's easier to see so let's go back here execute it again and you notice we get the exact same thing uh before that we just saw so the the issue really comes into play in that fast api doesn't necessarily recognize a put or a patch one way or another um it's not going to do certain things for us especially considering we're actually explicitly updating the data we're saying copy let's look at update data let's print that as well and see what happens let's go down to the bottom hit execute go down the bottom again so now look at what's happening we're only item item is that [Music] i don't know i don't know why showing that okay regardless let's um here let me try this let's try this and see what happens go down execute yeah it's still pulling in the default information okay whatever the point is that doesn't really matter what's going on the the issue that we're coming into here is we want to set exclude unset equals true okay now what this will do is this will look to see what's being passed in but more importantly it will look to see what's not being passed in okay so we're not passing in um tax nor are we passing in tags so if we want to update bar what this is going to do is it's going to look through each item that we've passed in here it's going to update with those but nothing else so if we hit execute now you can see we got bars we got the description as no we got the price is three we still got tags because just by default that's um you know that's that's a default parameter in our in our item model right here but what we didn't get is we didn't get an update of tax so we didn't pass in a tax and the default tax is 10 and a half but you notice down here that we still have twenty point two as our tax so this did not update and that's what exclude unset does so it allows you to pass in data now this is not gonna like this is no we're explicitly setting this to be null so it will read that as a null value and it will set that value to null in our quote-unquote database but what it's what it's also going to do is it's going to notice what was not passed in it's just going to forget about those okay so if we go through this again just to kind of summarize we are fetching the stored item this is really just you know error checking because we're using the dot get right here if it exists then great we're going to create an item based off of that stored item data otherwise we're just going to create a fresh one then we're going to we're going to create an update data dictionary where we pass in exclude unset that's the key part of this whole thing then we're just going to say the updated item we're going to take the stored item that we have we're going to copy and we're going to update with this updated data and then we're just going to take that data encode it via the json-able encoder that we've already grabbed and we're going to reset the item in the in our quote-unquote database with that value so bar actually is updated bar has tags if we look at it you can see right here this item here it had tags associated with it because we were creating an item instance and then we were passing that in okay so there's a lot of stuff that um the fast api will do for you but there's some stuff it won't um this this is one of the big things i actually when i was building um an app of my own and i was i didn't know about this this little piece right there and i spent a good half an hour trying to figure out what the hell was going on parse through things myself until i saw that you could just use exclude on set equals true so this is a this is a big time saver especially if you're gonna you know you can you can have put and patch methods together where one will replace the other like the put will replace data the patch will just update what you're passing in uh but this is a very helpful little tidbit okay uh next video we are going to start touching on a kind of like a little subgrouping of of topics that um uh they're called dependencies uh there's i think you know five or six videos that we'll do on dependencies and then we'll get into some other stuff okay i will see you there
Info
Channel: JVP Design
Views: 4,161
Rating: undefined out of 5
Keywords: fastapi
Id: _BeVJrQh5yg
Channel Id: undefined
Length: 17min 38sec (1058 seconds)
Published: Thu Jun 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.