Django Rest Framework for Beginners - Simple CRUD API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're going to create a basic api using django rest framework i'm pip installing django and django rest framework into my folder and then we're going to do django admin and we're going to do start project we'll call this one storage because we're making a storage api i'm going to put the dot at the end so it's installed into this folder and then we'll do start app and we'll call this api so now we have the boilerplate created for this project you'll see that i have the two new folders first thing we're going to do is go storage and settings we can remove our terminal scroll down to the installed apps and then we'll do api.apps.api config we'll install that which is the application we've just installed created rather and then we want rest framework because we're going to be using django rest framework this here comes from this file here there we can see api config once that's done you can close that out we're going to create the models first so we are going to have two models we have a location and an item so let's have our class location and this is models dot model and we're gonna have one field here we'll have location name and that's gonna be equal to dot chart field with a max length of 100 i'm also going to make this unique too then i'm going to have define a new function and the string representation and we're just going to return out self dot location name so when we look at this in the admin panel we can actually have the name rather than the id the next thing we do is another model which is the item so models.model and we're going to have item name which is going to be models dot chart field and this one will have a max length of again a hundred then we need to have the date added we want to know when when this item was added and we'll have models.date field and we'll have the auto now add as true which will add the date when it's created the third field is going to be the item location which is going to be our foreign key so this is going to link back to the first model that we created so models dot foreign key we want to send it back to location and on delete we're going to have models dot cascade which is going to just make sure that when we delete a location it will just delete all the items that are in that location as well and then once again we'll have our string representation and this will be return self dot item name please there we go so you can see we have two models and we're going to have a location where we'll be able to store items in and query down items per location as well the next thing we'll move on to is just the admin file and we'll import our models in here from dark models and we'll import item and location and we can do admin dot sites dot register our item model and then the same for our location one so now when we actually start this up and go to the admin page we'll see both of our models there so what we're going to do now is we're going to go ahead and do python3 manage.pi we're going to use that file to make the database migrations for the models we've just created our actual location at item and then we are going to migrate those over to actually create them in the database and this is also going to create the django basic user account too whilst we're here we're going to create a super user for that account for that user and we'll do just fill in this detail i tend to use the same for each one there we go so let's run the server now just check everything's working uh run server it's working here so if i load it up in the browser we can see this and if i go to the admin page this loads up try that okay there we go and we can see we have locations and items and we could create them in here if we wanted to but as this is an api we're going to move on from that so then we need to create two new files now let's close out the server clear this up and i'm going to use the terminal to create them but it's up to you how you do that and we're going to create urls.py and then we're going to create serializers dot p y as well so the serializer is what the rest framework is going to use to convert the django database model the data from that into something we can turn into json so that's done so i'm going to come now to the main storage file storage folder and i'm going to go to the urls.py and we're going to add in with include the actual api urls here so we're gonna do api and a forward slash we're gonna include in everything that comes from our api folder dot urls and this is the urls file that we've just created that is now still blank i'm just going to copy this to make my life a little bit easier i'm going to go into our urls.pi and get rid of include because we don't need it and we can get rid of these whilst we're here what i'm going to do first is let's create the serializers let's open up our serializers file i'm going to do from rest framework import serializers and we need two serializers one for each model you can have multiple ones per model but this is what's going to take that django model database model and turn it into the json data that we can work with so i'm going to create the first one as item serializer and this is going to be serializers.model serializer and actually need to import my models in here as well so we do from models import item item and also location so we're going to do class meta and we'll do the model is going to be item so it knows which model to take it from and the fields that it's going to be taking is just in this case all so i'm just doing that you can of course choose which fields this serializer returns back and you this is where you might have multiple serializers so i'm just going to copy this because the next one is going to look very similar but except it's for the location so i'm just going to give it a different name and tell it to point to the other model now that this is done i'm going to go back to our urls file and we're going to actually create the url structure for our api here we're going to have four endpoints one each to view the all of the items and all of the locations and then one each to view the individual uh item and location if you wanted to and within one of them will also also include a query parameter and we'll do that in the views so we're going to say from dot views imports now we haven't actually created these views yet but we will do item list item detail location list and location detail so these are the views that we will be creating in our views.pie for now we'll just do path and it's going to be item like this and we'll have item list dot as view because these are class based views so we need four of these and we're going to do for the second one we're going to have the an integer we're going to be expecting which is the primary key for the item detail so that can go in here we'll do location list location detail just filling it in here we'll change this to location and we'll change this here as well and add that on the end so now we've created the url patterns this is what's going to go into the browser when we actually look for this data we have to now create these views so let's go to our views.py file remove all of this because we don't need it because we're going to be using the rest framework views we're going to import generics which is going to contain all of the api views which will handle the crud functionality for us we also need to import the models so from models import item and location and we also need those serializers so from serializers import the item serializer and the location serializer so the first view we wanted to create was item list just double check that's what i called it yep item list and this is going to be generics.list create api view so it's this view we're going to be using to be able to create items for our api this is also going to be slightly different because it's going to be the one that handles the query parameter so we can see all of the items per location so what we want to do is want to say our serializer class is the item serializer and we have a new function to get the query set the query set is the data that's going to be coming from the database so our initial query set is going to be equal to item.objects objects dot all so initially we're gonna say we want everything we're also gonna say that if we have a query parameter so let's go ahead and do let's call it location is equal to self.request because when we hit this api view we have access to the request object dot and it's query parameters dot get we want to get the one that is going to be called slash the location and from here we can say if location is not none we want to actually filter down the data so the query set is going to be equal to the original query set dot filter and we want to say item location is equal to the location so this is what the data this is the information that's going to be passed via the query parameter on the url we're going to filter on the item location which is here item location for that model a lot of use of the word location but hopefully that makes sense and then we're going to return the query set back out so this is the most complicated view that we're going to create the others are much simpler so we'll say item detail detail and this is going to be generics.retrieve uptrade update and destroy so it's on this detail view details put it right is the one we're going to be able to actually update and destroy and delete so this is our crud c r u and d it's just handling it in these views so we just want the serializer class is equal to the item serializer and the query set is going to be equal to item.objects.org because it will automatically get the right one for us when we go to this view giving it the primary key now we just need to do the same again for the location so i call this one location list and this is going to be our generics dot list create update view serializer class is our location serializer and the query set is going to be the same except instead of item we want to query the location database object there the database table and the last one is going to be the detail so we can just add this in here and this is going to be the retrieve update destroy view so these are our views that we've created so now what we can do is let's just go ahead and try running our server and see what i've forgotten to put in if anything or something api slash item i spelt objects wrong there we go there's always one thing and there's our item list so this would be the json representation but to add items we need to add location so let's add a location first we're going to go to location we're going to say our storage unit and we can post that location and you can see it's created it and it's still there look let's add another one let's add in our shed and our garage let's add in our final one and let's just call this one under the stairs post so we're going back to here we can see here's all the locations we've created if we were to put the id at the end of the url up here you can see that we get the location detail which is this view that we've created here and it's showing us this location from here we can update the name and we can say let's change this to capitals for the sake of it there we go we can use put to update there we go or we can delete now you can send all of this via the api as well why requests or something like that so now we've done that let's go ahead and create some items so we'll say our sofa is in our storage unit so is our tv stand make that tv stand there we go and let's say that our bike is in the shed and our car is in the garage and our camper van is also in the garage because we have a massive garage which is great for us lovely so you can see here's all of the items this is the main list you can see the date that they were created they were added sorry and the location that they are in now i said that we created this query parameter here uh with this here so all we need to do now is pass this in so we're going to go question mark location and this will allow us to query down every location item that is in location number one you can see that we have two here i change this to location id2 we get this back here so that's how you can easily and quickly create a basic api crud api with a query parameter option in django using django rest framework
Info
Channel: John Watson Rooney
Views: 23,110
Rating: undefined out of 5
Keywords: django rest framework, django, rest api, django rest framework tutorial, python django rest framework, python crud api, django crud api, crud api, john watson rooney, django api, django api tutorial, api class based views
Id: OJdFj5hPAKs
Channel Id: undefined
Length: 14min 22sec (862 seconds)
Published: Sun May 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.