Django Rest Framework | Serializers & CRUD

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so this is the start of a new series I'm working on covering the Django rest framework so in this video what I'll do is introduce you to what it is and basic concepts like serializing data and rendering out in JSON format and create update and delete functionality so we'll cover that in this video later on in the series we'll go over things like token authentication filtering data and much more so make sure you're subscribed and watching for that and if you want to actually use what we built today we're actually going to apply this in a real app that we'll follow up with in two different videos so we're gonna build a to-do app with one using vanilla JavaScript that will call this REST API and another with react Jas for anybody looking to integrate those two so we're gonna build that out and it's a good way to actually understand what we built today and just to apply it and understand that crud functionality so what's a REST API and why might we need it a REST API is basically a way to provide data in a unison format so typically JSON data and it allows multiple applications to interact with one central hub so REST API will provide that data but if we want to allow another user to interact with it we can just give them access and they'll see something like this right here in JSON format and another example might be if we want to build out an app with react or maybe a mobile app with flutter or react native these will require some kind of a REST API to tap into you so we'll have to get that data from somewhere which is what we're gonna build out today and a way to interact with it okay so before we get started I want to show you the jingo rest framework documentation and this is a great resource for beginners and anybody working within it because there's a lot of concepts to cover here and I still reference this daily so it's a really good resource on all of the topics and everything that we need to do with the Django rest framework so the Django rest framework is just a way to build out REST API using Django so it's just a toolkit that we can work with and it gives us a lot of extra functionality to be able to do this so in order to install it we'll cover our application here in a second but before this will actually install it in a minute but that's how you install it so pip installed Django rest framework and we add it to our installed app so before we do that let's go ahead and look at the app that I have setup here so I just created a simple app and/or a simple project and the app within it I just called API so normally you can do this with a by creating your own folder and creating all the files but I just wanted to get a quick start so I just made it its own app so the app or the API app will just hold the model so this is going to be a to-do app so the API is gonna be based on a to-do app that we're creating and that to-do app is just gonna have the model of tasks and that task will have a title so something like get milk or something like that and is that task complete so that's just a boolean field and if we look at this it's configured in installed apps and in our URL path anything with the forward slash API will take us to the API app and in here we just have that URL pattern and for now I just have a view called API overview so we'll go ahead and change this and this view is just gonna show us all the URL patterns that we're gonna have so it's just gonna be just a dictionary a JSON response and it's gonna show us some data so let's go ahead and actually run the install and one thing I want to make clear for anybody here that might think this is a basic Django tutorial it's not this is for the Django rest framework so this is for building out api's I just wanted to make sure nobody used with that so let's go to the documentation and run this pip install so I'll just copy and paste that and in here we'll just run that install okay so now that we have that let's go ahead and add it to installed apps and I'll show you what we have in that URL path so let's go ahead and bring this back in here I have two screens open so kind of acting funny so we just need to add this to our settings da py installed apps so go ahead and paste that in there and that's it so now we have access to everything that the Django rest framework gives us so let's go ahead and turn on our server and let's set up some URL routes so Python managed on py run server and I'll just show you what this view has here so we'll close out settings close out this URL path and we're just gonna render out this view so if I open that up and go to that base pattern now because we have that URL pattern we're gonna get this page right here because in that urls that py file the only route i have right now is to forward slash api so that's a simple JSON response and in here what we want to do is convert this into json restaura yeah json rest framework responds so an actual API response so let's go ahead and run a few imports here so if you go to the documentation and look into tutorial and request and response we need access to this response method right here and we also need to add a decorator to make sure our view is a view that can access the methods that Django rest framework gives us so we can use class-based views or function based views and because I'm using function based views we need to add this decorator right here so let's go ahead and run those imports so I'll go ahead and just paste them in and I'll explain what's going on here so we're gonna import from rest framework decorators API view and we're also gonna import from rest framework response we're gonna import that response method so what that allows us to do now is if we go here it allows us to use this API view decorator so if I go ahead and paste that in that will now allow us to have the function that Django rest framework gives us so we only want to allow the get method here and we also need to change this JSON response to a Django rest framework response so with that being said what we're gonna do now is actually return some URL patterns that we need to set up so this view is meant to just be an overview so anybody that wants to work with our REST API if they want to see the URL routes they can go ahead and just do API or our URL and then API and they'll see a bunch of URL patterns so I'll go ahead and paste that in and we'll go ahead and just return that so I'll just indent this and this is the list of URL patterns we're gonna make now so we're gonna create a list of to do objects that we can use a detail view which will allow us to just see one object based on the ID that we pass in a way to create update and delete so with that I just need to go ahead and return this so if we just eliminate all of that and return API URLs you're gonna see a different response now in in our browser here so let's go ahead and refresh this and this is going to look a little different so we can work with it just like we would any other JSON response but it gives us a little extra detail on what's going on here so that's the URL patterns that we have we used a get method on that and we can view it just as a normal JSON response if we change that so if you look at the URL pattern that changed right there so this is what we just set up and now what we want to do is work with serializing data so what that's going to do is allow us to return any model object here in a JSON response so it's just going to be a JSON object so in order to do that what we're gonna do is create a file called serializers dot py so go ahead and save that so serializers py and in here what we're gonna do is create a class and specify which model we want to sterilize once we create that in here we can start using in our view so in serializers I'm just gonna paste in another import and this is gonna be from rest framework import serializers and now let's just go ahead and import our models so from models because we are in the same path as our model we're just going to import tasks this is the model that we want to serialize and if you've ever used model forms it's very similar to that where we create a class and I'm gonna follow the method or the practice of using the model name and just saying serializer so it's gonna be task serializer and we're gonna say sterilizers dot whoops put that outside there let's paste that back in here and that's gonna be sterilizers dot model serializer and you can you can sterilize any form of data you can actually create custom data here but we want to serialize a model so that's what we're gonna work with here and I spelled that wrong originally so we have our model serializer and we just need to do class meta and we need two attributes just to make this work we can add more later but we first need the model we're gonna sterilize and that's going to be task which we just imported and we need to know how many fields do we want to display so which fields the wheel we want to allow to see so fields we're just going to use we want to display all of them so we're gonna do a double underscore all and double underscore so that will serialize an object so we created our serializer and what we need to do now is import our serializer into the view so i keep misspelling steriliser so what i'm gonna do is just go ahead and paste this in here i pre wrote it from dot serializers so from this file we're going to import that task sterilizer so we're gonna do import and we're just gonna get this serializer and we're gonna work with task sterilizer so let's go ahead and make a list of responses so we can see all of the data in our database so I actually went ahead and created a few tasks so a couple of items that will be rendered out our to-do list so this one we're gonna call task view so just being a function and we're gonna call this task list and we're gonna camelcase that and let's go ahead and do requests and we'll just return oops keep spelling that run so we're just gonna go ahead and return that response so return and that's gonna be this response right here so let's go ahead and paste that in and let's actually go ahead and get these tasks so we're gonna do tasks and then that's going to be equal to tasks da objects all so we're gonna create the task and what we're gonna do now is set the serializer so we're gonna say serializer and sterilizers and that's gonna be equal to tasks serializer so we query the objects and in here we're just gonna throw the objects into that serializer so this will take care of actually sterilizing that data so we'll just set the tasks there and now we need to specify do we want to sterilize one object or do we want to have a list of them so we're gonna say mini is equal to true and later on we'll actually set this to false when we're just querying one item so for serializer what we just need to do with sterilizer data and that will now query our database serialize the data and return it in our API response so now we need to create a URL path for this and before I forget we need to import the models and put a decorator above our views so from models import and we'll just do tasks so we need to import that before we can call it and let's go ahead and put a decorator above here so we only want to allow a get response or a get call so let's go ahead and finish up our URL pattern so in our URLs let's go ahead and grab this path right here and we'll just work off this so we're gonna call this task - list and then port slash and then we're just gonna use this view so we'll just go ahead and paste that in and we also gonna just name it task - list so I want to change that and now if I go to our browser here and I do API forward slash task - list so what we have right here so now if I go there we should see a response of all the objects so this is all of our data and you can see we have the ID title and the completed status so we can actually begin making API calls and working with this data now what I want to do is return a detail of view so right now we're returning a list but let's say I want to return item number one or item number three on the list so in order to do this we're gonna create a detail view and in here within our view it's very similar to this we're just gonna get the task list and I'll go ahead and just paste this and start working with this functionality and changing up a few things so this will be changed to task detail and we want to pass in the primary key or ID so same thing there and we'll do tasks down objects don't get and this ID will equal to whatever we pass in and the serializer so we're using the same serializer we just query one object and we change many to false so this means it's just going to return one object based on whatever we're querying so for this we're just gonna do task detail so I'll just paste that and we'll just change this to detail and at this point everything's just gonna be rinse and repeat until we actually get into updating items and deleting so detail and we'll use this view and change that so with that all I have to do now is use task detail and I actually forgot one part here so let's fix this before we get an error let's go ahead and pass in that primary key so we'll just do PK and that's how we're able to access it right here so I'll just do forward slash and I know everybody has their own method of doing this I just for tutorials I keep it simple like that and now if I pass in the ID of one there we go and if I change that to two so that's given us our object information so this is if we want to just retrieve one item so let's go ahead and actually handle create functionality update and delete so we'll just finish these up and that'll be the end of the video so let's go ahead and copy this and we're gonna work with a detail view and we don't need a primary key and we are gonna send post data so this view will only allow a post method so in here what we're gonna do is just that serializer is going to be equal to tasks serializer and we're gonna pass in some data so the data is going to be equal to request dot data so if you're familiar with model forms normally we do something like request on post but because this is an API view we have access to request data and if you look at the documentation basically request on data sends us a JSON object and that's how we can work with it so we're just gonna go ahead and throw that in and we can actually save this like we would standard any kind of model form or just a form so we're just going to do if serializer dot is underscore valid and I thought that was pretty cool that we can just call it just like that like a form right there so we'll just do if steriliser is valid serializer dot same so if it's valid it'll actually send that item back to the database and save it so I'm not gonna work with air handling or anything like that so let's go ahead and save that and we're gonna make this task create we're going to name the view so the task create and in our URL patterns let's go ahead and use task list because we are not passing an ID so this will be easier to copy off of so we're gonna pass in tasks create view and we'll just do will just change up all these and now what we can do is I'm actually going to copy this object here because we need to send a JSON object and we're gonna change this to create and let's go ahead and remove that so now all I have to do is throw in an object I'll remove that comma and we'll just say new item so if we send this as a post method and I've seen a lot of people use a postman for this I'm just gonna use this just because it works the same might not work as good but it'll get the point across so if I send that post data we see that new item returned because we after saving that item returned the data but if I look at the list and refresh it there we go so we sent that data into that view and the steriliser took in that data and went ahead and saved it so now let's just do update so if I go ahead and copy this this is also going to be a post request so we're going to leave that decorator as post right here but we will change this to update and we also want to pass in the primary key so PK and with this one what we want to do is actually grab the item first so we'll go ahead and throw that in right here and that's going to be tasks and we're gonna get the object that we want to update data is gonna equal to request data but just like the form we're gonna set the instance so instead of creating a new item we're gonna set instance to and we're gonna update the instance of the item that's in the ID so serializer instance is that task we go ahead and check if it's valid and save that so let's go ahead and now change this path here so we'll go ahead and change that to update task update and I think you'll start noticing a pattern of what's going on here so it's a very standard to how you would normally handle crud in Django just a little bit different with things like the serializer and request on data tasks update let's go ahead and fix that update and I think that's everything so now if we go to task update and we'll just update the first item actually let's update this one right here so item number six so task update will pass in the URL and now we want to update it so we have the item and let's just say updated so if I hit post it takes the post method and item is now updated so there we go update so last let's just finish up the delete method and then we can actually move on if you're interested in actually using this information or using this API in a real application we'll actually do this with a to-do app on the front end so let's go ahead and finish up with a delete method and delete it's going to be let's go ahead and paste that in we'll just change this to task dot delete and instead of a post method we're actually gonna take in delete so that's its own method and with this what we want to do is grab the task and we don't need a sterilized it we're just gonna do delete so task and task dot delete so just the delete method that's pretty standard we're just calling it on the model and all we're doing now is creating a URL URL path for that so let's go ahead and throw that in and delete and throw and delete there and pass in the view so let's go ahead and try that out so now if I just do task delete what we want to update our delete let's say the first item so let's just do item number one and see how this works and I think I can throw in the delete method yeah so I'm gonna use the delete method again you'd normally send this from the front end so you'd pass it in the request once it's let's see what's going on here did that delete it okay so that did delete it and it looks like our air comes in this response and that's because we're calling sterilizer data that was just a copy and paste error so I'm just gonna return this message and say item successfully deleted so what we're gonna do in the next video before we continue on with a jingle rest framework series is create this to-do app in two different projects so we're gonna call the REST API that we just built out and rendered this data and actually handle that crud functionality with just JavaScript and then we'll actually do it with react for anybody that's interested in that so we're gonna work with this data it's gonna be a cool experience if you want to actually go through that whole process so make sure you're following don't forget to subscribe and thank you for watching
Info
Channel: Dennis Ivy
Views: 193,302
Rating: 4.9487543 out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, DRF, Django REST Framework, Rest API, Django, Serialization, CRUD
Id: TmsD8QExZ84
Channel Id: undefined
Length: 22min 39sec (1359 seconds)
Published: Mon Feb 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.