Django rest framework generic views, Serializers, and CRUD.Django rest framework project tutorial[8]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys welcome back so in the previous set of videos we have been able to register verify and login a user so if this is the first video finding I highly recommend you check those videos out and if you enjoy this type of content please consider subscribing to this channel so now we are going to go ahead and add a beautiful user to record the expenses so will like walk out the whole crud feature for expenses for a specific or a logged in user so for us to use that I'm going to come here so I need to create an app to manage our expenses so here I'm gonna stop our server in a bit then Python monitored key wine stop app so I'm gonna put this on expenses alright so let me run back this server so once we have that now you can see that you have an expenses folder created so we need first register it so I'm gonna bring this one in then I'm gonna change this one to expenses alright so once we have that now we need to define our expense mode also in the models with py right here I'm going to define a class this is gonna be expensive just going to inherit from mother's murder so yeah now we can define our attributes for our model so the first thing you are going to have will be the categories so I'm gonna have a constant here code category options so category options will be a list so this is gonna contain very delicious that so categories we support on the application so now I'm actually going to use this syntax because I want to be able to add many so I'm gonna have the one is going to be expenses for online services online services and then can we bring this one in again so I'm gonna duplicate this so the next one will be maybe travel the next one say travel here the next one will be hit say we are spending on food and have like food in there let's say we spend on rent can have like renting there let's say spender and can we keep at us for now but what we can add more here for now let's keep it simple like that so now we have cut aggressions defined so now to have a category we're going to have a category attribute so category this one is going to be equal to models put Chatfield and then we are going to provide choices so the choices are going to be like this own our own soul and provide choices equals to category options bring this one in like this and then we can add since it's a field we need to provide the max thing going to be 255 all right so once we have this now let's define the other ones so the next one is gonna be the amount so the amount is going to be a decimal food because we are going to be working with money here and the decimal field is actually most suitable for this some people use the fruit field but I would say decimal fields provides you like a lot of methods to manipulate the data and you will mostly run into a lot of problems we take a floating floating decimal values when you're using fruit points so this is my field this one we'll be using here so right now it's my foods expects us to give its max digits so you can say maybe a maxi this will be 10 because the reasons might be that rich so then we need to also provide the decimal places so this provide consistency to like how how jungle will be formatting our money so now we can have like 2 that should do so have the category now the next thing are going to have is the description so a user can add rake a an optional description so this is going to be the text field and we don't want to like enforce any validations so it can be empty so the next thing you're gonna have is the owner so the owner will be someone who owns this experience so since if you think of it our application is going to be having tons of users so we are going to need a way of knowing who owns this expense say that we give them only what belongs then this is gonna be a foreign key so it's gonna be models at foreign key and foreign key actually takes in quite a number quite different things so one will be the two so two will reference to the model so the motor is gonna be our user murder would import that one in then we define and won't edit so we want when a user's are deleted we don't want to keep their their records so here you can do models put cascade yeah so for the most part if you want to do like a soft delete which is keeping the record even when it's deleted you can look at the custom queries API so that we'll be able to show you how you can do soft soft digits so you define when you do something user boots off delete user that did it those can mean two different things for it's very simple to use so from not fun so from Broadmeadows yeah so from authentication both murders input user so once we import user now this would be okay so we need also keep track of the date so the date will be a date field this one we want it to be required so you can say now equals close and say blank equals false it should be a date so once we have those two that actually the ones we are going to keep for now now we need to run our migration so here you know what time you know I'm gonna stop it and then run migrations don't make migrations with migration then we need to migrate so that's initializes our that sets up our tables so now let me bring back the app so now we need to set up our endpoints so in the views that py we are going to be using some generic classes that will enable us to scaffold out these points really easily so this important Eric's so from rest framework the generics imports so one we are going to have an endpoint that will do a distinct we should be showing my own my expenses and then creating new ones so it's great and we are going to have another one that we'll be able to view the detail do delete a record update a record and all that stuff so this one is gonna be called retrieve retrieve update destroy a behavior all right so here gonna set up a class it's gonna be good expense list so this one we inherit from me actually make this one yeah it's one we inherit from list create and now here we need set up quite a few things so one will be the serialize a class of course or ERISA underscore class so if you're gonna put this on something like expense list serialize I so I'm gonna want call it expense serie Eliza Eliza expense area Raisa fine we need to import it we haven't created it but we will in a minute so dirt from Sri rises what Savior ss-sorry entices import it's gonna be cold expensive ERISA so actually it's created so it's obviously regulated py file and in here we define a class expenses so your eyes this is going to inherit from Sri arises but mother serializer okay so this inputs a right I'll go up here so from rest framework input gets its area Raisa alright so in here we need to define a classmate of course otherwise we are gonna get a little you're gonna hear a lot of yelling so yeah this it's a motor serezha we need to define which model it is so model will be experienced so let's bring in experience from Tata Motors import experience so now down here we need to define a fields so here if you look at what we have we are going to need the date we are going to need the ID we are going to need the description we are going to need the amount and the category okay okay so for now I wanted it to be read-only so I'm going to say I want the ID to be read-only so here okay this is fine so I want daddy to be read-only I but econo as well remove it from here really doesn't matter so as we have this one set up let's take a look at our models again so we are not missing anything the user no we don't need it all right so this looks good to me so now if we go back to our view we need to define some few things some more few things one of them we are going is we are going to need to override the function the method that creates an instance of this and then tell it which owner means in this case which owner it will be and for us we want to set it to the current logged in user so for us to do that we are going to override a method code for concreate perform create and now in here we want to return Terrorizer but save and we want to save with owner equals self got request which user so this value that would be okay so another thing we need to do is define a query set that this class can use to get our objects so here kind of query set so this one is gonna be actually should be query set no telesco so it's gonna be now we can use our models of wrong both motors import experience this one is gonna be expense but objects float all for now but right here we also going to override a method called get queries it so get query set so in here we can now interrupt our query set and filter it so you can return so the query set good filter so one filter by owner equals the current logged in user who'll be himself but you quest books user ok so this will ensure that the user gets only the expenses they created alright looking good looking good so once we have this actually we need to define the authentication the permission classes just so users who are not authenticated should not access this so in sorry I'm going to input something from restomod click on rest framework import permissions okay so right here can do permissions equals so it's gonna be a tuple now we can say permissions what is authenticated all right okay so this will provide users who are not educated from accessing this these endpoints so I'm gonna do the same thing for the for the details endpoints so I'm gonna copy that then I'm going to rename this this will be expensive detail we're gonna add API view to this then I'm gonna do the same thing up here okay so in the details we basically need to do what we need to do basically is define a lookup field lookup field lookup underscore field and this is what Django will be using to to know how to query for the details of an expense so this one's gonna be inherit inheriting from retrieve create a retrieve update destroy a behavior so the lookup field is gonna be the ID because we know that our IDs will already be Nick okay so now notice how the permissions basically we are we want the user to be indicated so when it comes to the detail we are also going to want the user to be authenticated but then be the owner of that so just shout being authenticated would not be enough so we need to define a permission that will tear that someone who created this is someone who created this is the actual untrained to access it so users work and hitted should not get paid the expenses that don't belong to them although occurs that will do that but then it's good we define a custom permission so I am going to create a file put permissions let's see why so in here we are going to import a tivity from rest framework so from rest framework import Commission's so we set up a simple class so this one is gonna be called its owner so this will inherit from permissions based permission so now we need to override one method called has object permissions will undo death has object permission and now you can see this one gives us they request the view and then the object itself so what we want to do is we want to return object the owner it should be about yourself it should be okay so the object dot owner will be equal to the quest but user alright so this will ensure that this from the object we are trying to access the owner on the object you are trying to access will be equal to the current user I was trying to make the request here alright so since we are returning okay so since we are returning an expression we need to use those two so let's import it in in our view so here we can do from with permissions import owner so now he ain't no permission classes you can also add is gonna put a comma we should do so everything else looks good to me so now we need to register these in our euros so here let's create a urs the purifier so now we need to define our own input path so from jungle euros import path we support the views of chrome dots and put pews just a nose many who can input all of them so here we can find our URL patterns at stats URL patterns so the first one is gonna be path this one is gonna be going to the index so I'm sure going to keep it like that so this is going to now be rendering a viewport views expense lists a preview and of course we put as you and then we need to give it a name so name will be expenses okay so I'm gonna copy this expenses and slash the ID and then it's gonna be called ID and then this will be the expense alright so once you have this now we can hook it up with our main application routing so in yours at pew I wanna copy this this is going to be going to expenses and then this will go to expenses of post okay looking good so this let's save it up and then we run our or server turn out make sure you restart it so you see we have an issue so let's see how we named our serialism here I scored expenses three ERISA and then in our views let's see how i am putting it make sure it's the same okay supply is back up so go back here and reload now you can see that we have the expenses so if you take a close oh yeah so we should be having more here so let's take a look at at our euros so this one should be going to the details of use that expense detail the behavior all right so let's reload and now you can start we have more here okay so looking good so let's test taste out for the gate so right now we try to come to try it out and execute alright so you see we get this error here so this is about to use are not being logged in so we need to first they were user login into application but for now when I come to authorize you can see that it's by default using a mail password but you want to use it to Penn State so go to our our settings rpy so down here we need to define the Akaka settings so to create soda under spa settings so this is going to be equal to and dictionary and then inside here we need to define the security definition so write security underscore definitions and then it's also going to be a dictionary so in here we want bearer many we are going to be using tokens so in here when they type so the type is gonna be an API key and let's don't change this this is standard so that would be the API key and then the name will be authorization okay and it would keep this and then need to have an in so it will be in later so once we have this and go back so it should be swagger underscore settings so once you have this and go back see we are back so now when we click authorize because that we need to provide the the berry-picking so right now I'm going to come to the login endpoint and then I'll login let's see if this user has a token try logging in so these two that exists I'm going to use my own use I created earlier there's gonna be a Gmail then this is gonna be in my password like this so if we clicks execute we get a token so I'm going to use the access token here so I'm gonna copy this one up to here and then here you know thrice I'm gonna come in and write bearer in the token then click authorize alright sorry Mick kurup's that's once you come back to the gate and then I'm going to execute it you see that we get an empty list looking good so this is what we want is what you wanted to illustrate to create Sun so here I'm going to come in here and then this check for a validation so if try to execute you see we get amount is not valid so let's change it to something like that then this security see we get the new creative one let's do that several times and then let's try to get all so execute see we get a lot of them so we laid out but pagination to this but for now for now if we go too late the detail let's a want to view the details you can see that from the lookup field it knows that we need to give it an ID so let's say we give it like one click execute so you get only that so if you want to like edit it you can come in there and then notice how we need to like we can be able to change both this so they say the amount now we want it to be like 100 how a million that those are four zeros five six a median with the one with the ID one so here we can put like one so if we take a look at our list which should be here let's see so if we take a look at our lists the one with one mainly will be I think the one with Ritalin actually all of them have like 300 thousand so now if I edit this is it so if I edit this and then execute you see that it's created and then we have a new amount so if we come back to get one so expense says get one and then execute it you should see that the amount was changed and all that stuff so meaningless all our endpoints are working and you can see how simple it is to set these ones up using the boost API view classes that they provide us so like what they did it you can delete and then it's gone you get a no content response so everything is working fine the next one I will be showing you how we can add pagination to this one if you enjoyed the video give it a thumbs up consider subscribing to the channel and I'll see you in the next one bye
Info
Channel: Cryce Truly
Views: 13,848
Rating: undefined out of 5
Keywords: djangorestframework, restapi, django, rest, web services
Id: B3HGwFlBvi8
Channel Id: undefined
Length: 22min 10sec (1330 seconds)
Published: Sat Jun 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.