Django REST Framework Full Course For Beginners | Build REST API With Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I feel like this would have been easier with flask

👍︎︎ 25 👤︎︎ u/West7780 📅︎︎ Jan 03 2020 🗫︎ replies

I've been working with Django REST Framework on a recent project of mine, and this is the first time I've ever worked with any kind of Web API. It's been really difficult for me, but I'm having fun at least :)

👍︎︎ 1 👤︎︎ u/statsboye 📅︎︎ Jan 03 2020 🗫︎ replies
Captions
highlight the words our ways and welcome to Django rest framework course for beginners so before starting our main talk topic I highly request from all of you to please subscribe to my channel and also like the video okay so let me just talk about the outline of this course so this will be a 1 video course that we are going to cover a lot of things in Django rest framework so it will be more than two hours training course so the first part we will talk about Django project setup and also after that we are going to have introduction to Eliezer and we will work in the third part we will work with model serializer in the fourth part we will be talking about function based API and how you can work with that and the fifth part we will introduce API view decorator and function based API view and in the sixth part we will work with class-based DPI view in the seventh part we will talk about generic views and make sense and also we will work with Jarek views and also make sense after that we will talk about different type of authentication like session authentication basic authentication and also talking a tenth occasion in the ninth part we will have introduction to view sets and also routers in the tenth part we will work with generic view 6 and in the last we will last part we will work with the model view sets so this will be a one video course and also you can check video description I have added a timestamp to the d stable that to the table of contents for example if you want to leave escape one part you can just click on the time as time and you can check that part so I have added the video description if you want you can watch from the first it will be a good idea but if you want to skip any part you can just click in the video description you can just click on the part and it will bring for you so now we are going to talk about some concepts first of all what are restful api is now restful api acts as a translator between two machines communicating over a web service so if you are familiar with the concept of epi this is just like an epi but it's working on a restful web service and so web developers program rstp is that server can receive data from applications so these applications may be different for example these applications can be web apps can be native desktop application or can be android applications or can be iOS applications and also restful api is return JSON files which can be interpreted by a large variety of devices so now after this what is Django rest framework this is the topic of the course now for the Jaguars framework also we can call the DRF so which is a framework built upon the Django framework and it's used for developing restful api there are some reasons that you might want to use rest framework so the the first one is that it has a web browser well api and it is a huge usability went for their developers it has authentication policies that we will see in the video also it has the civilizations that support bought Orem and also non Orem data source and also it has an extensive documentation it has a good documentation and also it has a great community support so now in the next part we are going to set up our Django project for rest framework in this part we are going to set up our django projects so if you're new to django i have a complete course on django it's about more than four hours so django Treefolk course so I have added the link to the video description you can watch that and also before this if you open Django rest framework documentation or website you need to install Django rest framework so you can see rest framework requires a Python 3.5 3.6 3.7 or 8 and also the Django versions are these I'm using Django 3 for this course so for the installation you can simply pepp an install Django rest framework you can use this comment and it will be installed so I have already installed a Django rest framework and I don't want to install it again so ok now we are going to I'm using PyCharm ID so I've created a new project rest b6 and now I'm going to set up my project in here Django project I'm going to open the terminal the first thing we need I'm going to create my project in here so Django admin start start project and you can give a name to your project let me just give it my project whatever you want you can give you a project name so now I'm waiting for the project ok after that I'm going to change the directory to my project and now I'm going to create so first let me just do make migrations so python managed dot pi migrate so I have covered all of these in Django course I don't want to explain these so now you can see it's done and now let me just check that it is working or not let me just create a new session and CD my project and I want to run my server so Python managed drop by run server so if you seen here I have my project in here and now I'm waiting for this ok so now it is the server let me just copy and paste this URL so in here ok now you can see that this is the Django 3 and then style work successfully congratulation after this now we need to create we need to create app so let me just bite on manage start by start-up and I want to call it API for example basic now I am waiting for the app [Music] okay so now my app is created and if you see in here let me just show you okay now you can see that we have API basic app now after this the last thing that for the setup we need we need to create a super user so Python manage dot pi create super user okay so for the user name I want it should be my default user name and for gaming address you can give just an email address and for the password whatever you want and let me type password again okay now super user created successfully and now if I check this and if I write my username and also my password and go to login you can see that this is the Django site administration in this part we are going to talk about the building blocks of Django rest api and also we are going to talk about serializers so when you're going to build a REST API a Django rest api so there are some components and so the first one is models and now models are if you're familiar with the Django so models these are the tables for our database and also we have views so it is view function for our URLs and also we have URLs so this is the URLs for our for our view functions so these are the concepts that if you're familiar with Django basics you'll know about these so another concept that is in Django rest framework that's called serializer so now it's an important concept and what is utilizer so before sending data to clients we need to serialize it to jason an api is end result as always Jason and API is communicate with multiple technologies which take JSON as your input so now let me just show you a simple example example of this okay now I have a class models now in the class models I need to create my model I'm going to create a simple model called article and at harrods from models dot model okay now the first field I want I'm going to title it should be I'm going to create a simple model not complex model so the max length I'm going to give you 800 and for the outer models dot car field and the max length is hundred also I need a meal so models dot model and so sorry not models dot model models too hot email field and you can give whatever the max ten for example 100 also we need a date so it is a model start date I'm field and they are too now add make it to true okay so let me also write that my string so again I am I recommend you to watch my Django BC casts video because I have covered all about models views URLs and this part we are going to just focus on the serializers so let me just return solve dot title okay also an important issue so if you see Django rest framework documentation and if you come down we need to add the rest framework in our installed apps so I haven't added this now you need to open your settings dot PI and then in the installed apps you need to add this also we need to add our API basic in here it is our app okay so after doing this now we need to do make migrations and also migrate our this model that we have created okay now let me just open my terminal and let me create a new session so CD my project and now peyten age dot pi make migrations and after that we will migrate so now you can see we have created model article and now python managed dot by migrate so now I'm waiting okay now all migrations applied okay so after this let me just add my dis model in my administer on my administration panel so from okay from dots models import or tickle so from okay from morals import article and now we need to register this so that mean dot register dot sorry admin dot site dot register and we are going to register our article in here so also I think I have very moved from Django don't can't rip import at me okay so now if I check my admin panel so let me refresh this okay now we have articles in here and if you check right now we don't have any data in here so okay now it's time to talk about the serializers so if you see django documentation a generous remove documentation and okay let me just bring this okay now in the tutorial and in the serialization if you come down and so we have created our model and we have done make migrations and also we have migrated now we are in need to create our serialize our class now you can see you're selling that the first thing we need to get started on our web DP is to provide our way of serializing and deserializing our model and a stance into representation such as json so and also it's telling that we can do this by declaring serializer sterilizers that work very similar to django forms so if you are familiar with django 4 so now let me just describe this let me open this and now I'm going to create a new class that's called serializer so and my API b6 I need to create a Python file and I call it serializers like this okay so the first thing i need i need to from rest framework import serializer also i need to import my model so from dart models i'm going to import our article so i have just one model okay now after this we need to define a serializer class so i'm going to call it article for example serialize ER and now this class inherits from their differents utilizers so it inherits from serializer you can see so let me just so we have a model serializer if you see in here we have a hyperlink model serializer right now let me just choose serializer and we will talk about model serializer and a later part of the video okay so now when you define serializer that serializer you need to specify all of your fields that you have anywhere models if you're familiar with a django form so there were two types of forms in django we have a form class so using a form class you need to specify all fields if you're using your a form class but if you are using model form class you do need to specify for example these all you can just simply do that so I will show you that an example but right now we are going to just copy all of these files from here and the fields and we need to add in our article serializer and let me just remove this or to now add like this and we need to change this to serializer so because we are in the civilizer class and we need to change this to serializers like this okay so now I'm going to write my create and also update methods in here because when you are going to work with a serializer you need to add you need to add these two methods and here let me just if create so they've create and we have a validated data and now we need to just return article dot objects dot create and then here you need to add your validated data so validated data like this ok now the source for the creation now we need to update and return an existing article instance giving a given devalue the data so for that we are going to create a div update and now we need to create a nest and so instance the title validated data dot get so it is I'm writing these because we are using serialize ER but if you use model serializer you don't need to add these all but because I want to show you step by step so I want to do all of these so now we need to give our title and also we need to give the Aniston start title ok so now I am going to just copy this because we need for all our fields so we need for author and let me just copy and paste it in here also we need to copy and paste like this ok so also we need for the email let me just copy and paste in here and the last one is date copy and paste it in here and at the end we need to save our innocence so understand start safe and also we need to return the instance like this okay so now let me just open my shell Jango shell and in here let me just write or let me just create a new session because okay so CD my project white unmanaged our pie shell okay okay so now first we need some ports from API basic so we are going to import from API that API basic dot models we are going to import our article model and also we need to sorry no model so sorry it is API basic like this and also let me make it smaller oh okay also from API basic dot serializers I am going to import my article serialize err okay and also from the rest framework we need to import from rest framework dot renderers that renders import JSON render and from rest framework dot parser import JSON parser okay so now we have imported this now we need to create an article so let me just create an article and for the title I'm going to give it for example article title article title and also for them out or you can whatever you want for example I want to give it my name and also for the we have another field email okay hit enter and okay now I want to save this so a dot a dot save and we have our first so if we check our site administration so let me just and if we see articles okay so we have the first article in here now so when we call save when we say save it's calling this it's calling this create method now let me just create another one so let me just create this so sorry let me let me just create another one and I will be back okay so I have added another particle also now we have got some snippets some and article instances to play with okay now let's take a look at serializing one of those instances so I'm going to create a serialize er in here and it was article serializer so article serializer and we are going to pass our a article in here okay so now if you see serializer dot data you can see we have this data now at this point we have translated the model in a sense into Python native data type so excel dictionary now to finalize the serialization process we need to render this data into JSON so let me just create a content in here and now let me write JSON render JSON renderer dot render and I'm going to add the serialize err serialize er dot data and now if you see the content okay now you can see that we have our this serialized data okay this is how that we have worked with serializer in January more but it's just a basic concepts but we will learn more about sterilizers in later parts so this was just we have serialized and a model so a modern instance we have see realized but if you want to serialize a Curie set for example we have to article in our model and we have saved that now I want to do the serialization on accuracy now how you can do this it's simple easy let me just create another serializing in here article Syria Eliezer and and here we need to pass our article dot objects first we need to get our objects and also you need to add another parameter that is called another flag money to true because we are going to serialize a Curie set for that you need to add money to true so now you can hit enter and our serialize ur da the data and you can see that so now we have two data and a section you and this part we are going to talk about model serializer so if you see our in our serializer class class article serializer we are replicating a lot of information so if you see your Django rest framework documentation it's telling that it would be nice if we could keep our code a bit more concise and the same way that Django provides both for classes and model from classes if you're familiar with Django forms I have said that we had for class and also model for class also it's telling that the rest framework includes both serializer class that we have saw and also the model serializer class that we are going to create so now let me just I'm going to just remove all of these codes and this time I'm going to use model serializer and in the model serializer like model form we are going to create our class beta in here and in the class metal we need to create our model that's called article and now we need to specify the fields so the best thing is that you can specify the field for example I want ID field I want a title field and I want the author field so for example sorry all tour like this or for example right now I don't want email but you can so I want these are fields so if you if I open my at Django shell and in here the first thing I need okay so now let me just create a réaliser and article serializer now if i print the representation of [Music] my Syria my serializer you can see I have the same representation and this part I'm going to show you that how we can write some API views using our serializer class so for the moment we want to use we won't use any rest framework features we are going to just write the views as a regular Django themes so as you remember I have said that there different API views that you can use in Django rest framework so we're using function based API views and this is just Jang regular Jack of views but there are a lot of there are some more API views that we can use for example we have class-based views we have generic based API views we will talk about do's and now later videos so now our and the views now I'm going to open my views PI and in here I'm going to write my views so first we need some imports the first thing we need from Jango so dot HTTP I need to import HTTP response also I need to import JSON response and also I need from rest framework dot parsers import JSON parser also and from dot models we are going to import article also we need to import our serializer class so from dot serializers import article serialize okay now i'm going to create a article list so i want to list all my articles and also i want to create a new article so using api's so I'm going to def article list and it take a request parameter and now I need to check so f if request F request dot method so so if this is get I need to get my our all articles so you can simply write article dot objects dot off like this ok I want to get my articles from my database I want to save that in the articles now after this we need to convert our des we need to serialize our this article you can use Syria Syria wiser so Syria article serializer and we need to pass our articles in here ok because if you remember in the previous part we have said that when you want to serialize a Curie set you need to add manatee true because this is a Curie set and we are going to serialize a cure set and by this reason we need to add money to true okay and after this you can simply return a JSON response so JSON response and we need to serializer dot data and also we need to add save to phones like this okay so this was the request method now let me just write the also the post method so LF request dot method is post so if this is the case we are going to create a data and we need to use JSON parser to parse our data dot parse and we need to pass requests in here and after this now we need to create another serializer in here so serializer article serializer and we need to pass the data so data data like this okay now in the post we need to check that if our serializer is valid so and get we don't need but in post we need to check this if our serializer is valid we are going to save our serializer and also we need to return JSON response so JSON response and in here you need to add serializer data and also we need to give a status so it should be 201 it's an a created status so 201 is created status we are going to give this so in the else case so if it's not valid we are going to return again a JSON response this time we need to return serializer dot arrows so serializer dot arrows and also the status is 400 400 400 okay like this so now we have were created this and now we need to create this URLs so I'm going to create a new URLs in my API basic so and here I'm going to call it URLs and I need to open my mail URLs project URL and in here I need to include important clues so path and now I need to include my URL that I have created in my app so include so sorry okay now here I'm going to add API basic dot URLs okay and now let me just copy all of this code and I'm going to paste this in here I don't need this include I don't need this at Maine and I don't need this I just need let me just give this for example article or you can for example write epi whatever you want and also I need to import my view so from that views import article list and we need to add article listing here article less than here okay so now I think it's completed and now let me just open let me just go to article so now I am waiting for the refreshing of the server now let me run it again okay so now we can see we have these articles and here I have ID 1 I have ID - and like this okay so I now I have opened the postman and I want to hit this URL through postman so let me write a local host and port 8000 and this is our tickle okay so if I click on send you can see it's get and sending request okay now you can see that I have the data in here so you can see I have two data in here and this is article this is my two articles and so our API is working successfully and you can see it's 200 okay but if you see in here also I have a post now let me check that can I post true this postman and let me just change this to post first let me just copy one of these and let me just change this to post and in the body I need a row and this is JSON and I want to post mostly I should receive error so let me just copy in here let me give it ID 3 and this is for example article title 3 and you can give the author whatever you want for example okay so now if I click on send right now it's 200 okay let me just click on send okay so you can see we have an internal server error it is because if you see the documentation of Django it's telling that in here okay so not because we want to be able to post to this you from Klein so the postman is client that won't have a CSRF token we need to mark the view as CSRF underscore exempt so we need to do this this isn't something that you would normally want to do and rest framework views actually use more sensible behavior than this so for right now we are doing we want to do this but where and the further later parts we don't need to do this so let me just copy this and paste it in - in here also I need to import this let me just copy this and paste it in here so I need to import from view store decorator okay so let me just run again my server so first let me do this okay so now I'm going to open postman and click on send again I have internal server error let me just check Oh slash and you have okay so there is that we need to add another slash in here I think the sudeer so let me add and let me click on send okay now you can see we have 201 and it's created and if you check you can see we have an ID tree in here and if I again let me just click on it and if I sent here you will see that I have a 3 data so params now you can see that I have another data this is the new data and if you check your Django admin in here so right now I have to but even if i refresh this you can see I have another data even you can check this anywhere at me if you see in your admin panel and the articles you can see that I have another article in here okay so we have thought that how we can created a simple basic API and we have used postman and we have posted the data so now for example you want to for example you are in the article and basically in this part basically in this now I want to show you that how you can retrieve or update or delete this article we have saw we have get the article and we have post to it and now we want to retrieve the article we have a we want update the article and we want to delete the article and also we want for example if you write for example I want article 2 and if I hit enter I should get article 2 in here from the API okay so now for this I'm going to create another method in here and so I call it for example first let me add CSRF exempt and now let me article the tail or whatever you want for example I want to write like this and I want to give a request also I need a PK or primary key okay so now let me just first get my data so I'm going to create a try except so article I want to get my object of this time I want just one I want to get just one article so piqué piqué piqué so it's objects like this okay and now and except if it does not exist foreign for example our we need to throw an arrow so if article dot does not exist so if it does not exist we are going to return an HTTP response so HTTP response and the status should be 404 like that sorry after this now if request dot method is get so if this is the case if this is the case we are going to just let me just copy this and paste it in here so we are going to so this time we need to we don't need to this like this and we need to serialize our data so it's article this time not articles and also we don't need to add because we want to just Curie we want to just serialize one instance so you need to add money to true and after this we need to return a JSON response so let me just copy from here and paste it in here like this okay and also we don't need to add this also now if it is port for the update so l have a request dot method that method is but we want to create let me just copy also these from here because I have written these okay so I want to parse my data my request from the data and after that I need to serialize my data and in here also you need to add article so article like this okay now we are going to check that if it's valid so if our serialize is valid so if our serializes valid we are going to save and we need to return a JSON response and this time we need to just return a serialized data and if it's not valid we are going to add error so the error so it was the update and in the LF also we are going to write the deletes or request dot method is delete so it's simple we are going to just delete the instance of our article so like this the lid and we need to return HTTP response so return sorry HTTP response and status and the status as 2:04 okay now we are going to open our URL start PI and we need to create this so guys one important issue these these are function based view EP is but when we are going to use for example class based or January we don't need to do all of these because I'm going to do a step by step and I have done all of them okay so now you can give this first let me import article detail and for this let me just add detail an article detail because we also need to add our primary key so integer PK okay so now let me just now if I good to hear I should get the second oh sorry it is not it is detail okay now you can see we have the second and if I go to for example third it is the third and for example if I go to full I don't have any four so it will give me a tour so page not found but let me just open my postman and let me check from the postman so this time let me just go to the tail and I want for example the first one okay now you can see this is our first and if I go to for example the second this is the second for example I want to update so I want to update the data let me just show you the dating for example so let me just change this to put okay so I'm going to click on the body and in here row and the juicer for example I want to change this tree let me just change the article to update it so updated article and for daughter for example let me give it updated author okay now let me just send this now you can see 200 okay now if I check so if I so if I send if I get the second data you can see now we have this Dayton here so now let me just also show you the delete you can delete delete so now we can see we have deleted and right now - for no content and now let me just check my article get and the params now you can see we have just two in here and this part I want to show you how to use a PIV decorator and function based epi view so as we have saw the function based epi view so this was the function based epi view from the previous part now this function based epi view provides a set of sample decorators that wrap our function based views to ensure their receive and n stands as an instance of request rather than a usual Jango HTTP request and all and also it allow allows them to return a response instead of Jango HTTP response so also the benefit of this decorator repair view decorator is this that you can access to Jango where browsable API viv so for right now we haven't saw the Django were browsable API view okay now so the first thing I need I need to import this decorator so I'm going to use in my view open your view that PI and you need to from rest framework dot decorators import API view okay so after this now you need to open now in here you need to bring some change to your code so the first this is our get and post method so I'm going to remove this because we don't need to this this was just example that I have show you now we need to add the API view in here so add API view and and here we need to give the get and post and here okay now after this now if request dot method is get we are going to get our all articles from the database and after that we send tooth data to serializer and now in here you can see in here we respond we return a decent response now we don't need to return a JSON response we need to import the rest framework response so from a rest framework dot response import a response return response we need the data and we don't need to this okay so in the post method now in the post method this time we don't need to parse our data so like this I'm going to remove this and I need to just create my serializer article serialize a class and after that I need to get the data from the request so the requests object that I have given in here so request dot data and now this is the checking that if the serializer serializer is valid we are going to save this and we need we do need to JSON response because we are going to return a response and this time we need to also we need to import the status so from rest framework dot status and port status or we just need to from rest framework and word stands okay so from rest frame or import status like this okay so in here we need to give status dot it was 201 created like this and in the else case it's not valid we are going to give a serialize err err err and we need to give a bad request 404 so dot 400 bad request like this okay so let me just first just this right now we are not going to talk about this article details we are going to just see the article list and after that we will bring change to our article detail so this is just gettin post method now let me just open this so localhost:8080 so you not post article this is our tea color okay so now you can see now we have a chat profitable API so we have a Django a browsable API and you can see this is 200 ok and we have post and get and this has accept and you can see we have get and we have two data so with ID 1 and ID 3 and you can see that we have post in here let me just post a date I'm here I'm going to just copy this and paste it in here I need to remove the ID because I don't need to ID so in here I let me write a new article title 3 and you can be even in for example and let me post ok so now you can see we have an ID for in here posted and you can see this is a HTTP 201 created and if I get my data so you can see I have three data in here even you can check this in your admin panel you can see that we have another data let me just open my admin [Music] and let me login and if I go to the articles you can see that I have this was a new article title here that I have created if you open the postman also you can check this in postman so local host so local host 8080 article and if I see the data sending request now you can see that we have this ID for that we have created two Django browsable web browser on API so now this was for the first one now we are we need to bring change to the details and also we need to add the code for pot update or deleting the data so now and here I'm going to also change this to at EPI vu and in here we need to add the get also we need to add pot and we need to add delete so don't wait okay so first and here we need to retrieve the data so this is our article we are going to get and in here we need if we need to add it does not exist and we need to change in here just to status dot 4 dot status 404 like this okay and now if our request is get we are going to add our this article data to the serializer and we return not JSON response we just return a response and we added serialized data in here and in our port we are going to remove this JSON parser we don't - jsonparser we are we are going to get the data from requests or request our data and we need to add at this data to the serializer and now we near we're going to check that if our serializers valid we are going to save and we need to return a response not JSON response and so serializer data okay it's a correct and in here we are going to also return a response railroad and this is status dot 400 bad request and now for the delete so article delete we need to just return response and 204 status dot 204 not contain okay so now this is completed and now let me just open my okay now this time I'm going to get the detail so first one for example the first one okay now you can see we have the first one in here and you can see that this is the allow methods like the lead put and get so you can see we are delete we are get an and here we have put so let me just change this ID one and I want to change this so let me just add this in here and I want to change this to update it article title and also update it name and if I put okay now you can see that this is updated and if I get my old data so from the post from the article so let me go to article okay so for the first one you can see that this data is changed and if you see in your Django admin panel now it's updated article title so one important point that I want to tell you that you can see that in here we have ID title author but if you see our models let me just open and here we have title author email page this is because that in our serial Iser we have specified that we want these fields so if you add email field in here for example email and if you refresh the article so now our database is refreshing and okay now let me just open so now you can see we have also added the email field in here so for these two we don't have email but for this we have unable so this is because we have a specified and serializer so if you let me just comment this and if I save few words if you want to get all fields or you can simply write underscore all so now save this and now let me just wait for the reloading of my server and now if i refresh this okay now you can see I have the all fields even I have the date field in here so like this okay so if I use postman right now for this if I go to detail and first detail you can see that this is the first one and also okay so now if I go to detail so first one okay now I want to delete this so I want to delete the first let me just click on delete and delete so you can see now I think it has been deleted and yeah you can see 200 for no content I know if I get if I go to my again to my article so you can see I don't have the first idea here I have three and four in this part we are going to talk about class based API views so in the previous video we have a saw decorator's with function based views so this was from the previous part so we can also write our API views using class based views rather than function based views as we will see that this is a powerful pattern that allows us to reuse common functionality and also it helps us to keep our code dry it means don't repeat yourself so if you see documentation of Django rest framework for the class-based views exceling that rest framework provides an API view class which subclasses django view class api view classes are different from regular view classes and the following sees ways so the first difference is that a request and class-based view request passed to the handler methods will be rest framework request instance not a django HTTP request instance also the handler methods may return rest framework response instead of Django HTTP response so we will see that this also it is telling that using the API view class is pretty much the same as using a regular view class as usual so you can check the documentation again you can read then we are going to create our test class so it will be sweet easy so I'm not going to remove these quotes for my function based view I'm going to just create a class in here for this I will work on my coding so the first thing I need so we need to import this from rest framework dot we use import a P I you view and after importing the EPI view now we are going to create our class so let me just call it article API view now this class extends from API view okay now we are going to like that we have dead end a function based view we have list all articles and also we have post article so and here you can see we have in this article list we add functionality is that we can get all posts our articles and also we can post the article so now we are going to do this functionality and our test article API view class ok so you can simply write def get so for the get now we are going to just write def get and now it needs first parameter itself and the second one is a request because we need to add request in here now after this we are going to just copy the code from our from here so yeah from this section and this time we are going to just copy this and paste that in here so you just simply created we have accessed all our articles from the database and after that we have virtualized our cue reset and because we are going to see realize our cue reset we need to add money to true and after that we return a response of serializers our data this was for our I get now for the post so def post and also okay also you need to add request okay now for the post also I am going to just copy the code so from here now I'm going to copy these codes and paste it in this section okay so just we are going to serialize our data this time we are going to get the data through the requested data and after that we are going to check that if serializer is valid so if this utilizers but valid we are going to save the data and we need to return a return a response with that 201 created and if it is not valid we are going to return our response of sterilizers errors with 400 bad request so like this just we are going to do and for the article details let me just first we are going to just check this now I'm going to open my URL and start by because this time we are going to use a class and we need to import this and I'm going to just comment this I don't want to remove this and I'm going to create a new pot so or let me just copy this from here and paste it okay so article now this time we are going to use our class that we have created so article API vu and because we are using class now you need to also add as view in here let me just save this and okay now so I've saved this now I want to check my database now it's reloading okay now let me just check this and let me refresh this okay now you can see this is the same result and we are receiving the same result and you can also add the data so you can just let me just copy this I want to post data so that you remove the ID and let me call it new article from API vu and you can add for example auto whatever you want let me add API view and you can add an email like this and now I want to put post this okay now you can see I have ID five and if I go to the kit you can see I have these data also if you use postman you will receive the same result so let me just go to article and let me send requests so sending request and now you can see that this was the name that I have created named article from API V so you can see that using a class article we have we used an API class based API view and it's looking much better so you have a clean code and we have gettin post methods and this is much better than this function based views now for the article details let me just create another method so in here I'm going to create a class article details it extends from API vu and now in here because in here we are going to also update and delete our data so for this first we need to create a get object so also we need to give an ID in here you can give ID or PK primary key now let me create I'll try in here so let me just copy also not write all the code now this is let me just copy and paste in here and in here I'm going to return article and let me just make it tidy so Idid just simple we are going to get the data from our database and ok so after this now we are going to first get a create our get in here so def get and we need to past solve also we need to pause the request with the ID like this ok so now after this now we need to add some copying the codes from our function based views I'm going to just copy this and paste it in here so serialize our article serializer and ok so now in here we need to get the article so article and self dot get object and we are going to pass the ID in here okay like this so this was for the gate so first we have get our data from our get object after that we have passed data to the serializer and after that we have just returned a Django rest framework response now this was for the gate now let me just write the pot so the pot request and ID also I need to copy some codes from here so put and let me just copy all of this code and paste it in here and just one thing need we need to get our article object like this okay so first we have good dog article object and after that we have passed the data to this réaliser and now we have checked that if this visualizer is valid we are going to save that and we return a response if it's not we are going to just return serializers dot errors so this was for the pot and just the last one is delete so for the delete request also I need to add ID and for this also I'm going to just copy the delete code like this and paste it in here also let me just get this article object okay now this is we have our simple code for the class based view now we are going to check this and I'm going to just copy my this article details and let me open URLs by and import this let me just copy this let me comment this my function based and instead I'm going to add this in here so it's the tail let me change this to ID or you want to may you can use P key and now in here I'm going to use article details thought as view because it's a class we need to use as me okay now let me just change check this I'm going to check the detail and I want to get for example at the third one okay so now you can see we have the third one data and you can see we have delete and also if you seen here we have put for example I want to change this data the third one so let me just I want to update this and let me newly updated data you can name for example up dated new name you can give for example an email address and if I put you can see now it's updated and you can see it's 200 okay and if I see now let me just check my article my so the third one is you can see it's updated namely updated data and if you see in the postman detail for the third data oh sorry okay so we have get okay now you can see for the third data now except dated also you can do a plate from here for example let me change this to put and and I want to update the second data for example or I want to update this third again so let me just copy this and so from the top I'm going to click on body row let me add this data and change this to JSON and so update it from post one for example and just I want to update this and let me just click on sent it okay now you can see we have 200 200 key status and if you see now it is updated and if I check my in here so let me go to the tail and the third third one it's not that yeah it's the tail third one you can see updated from postman accepted also you can delete for example I want to delete this data you can just click on delete and delete and now if I go to for example article so I don't have the third data in here and also if you check in the postman sent so get changes to get and send so because we don't have the third Annette's we are receiving attribute error so if you go to for example article and if you send APA request okay now you can see in here also I don't have the third data in a previous video we have talked about class-based views and Django rest framework so this video we are going to talk about a generic view and also make sense in Django rest framework so if you see Django documentation and for generic views it's telling that one of the key benefits of class-based view is the way they allow you to compose bits of or usable behavior so rest framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns so we have saw that this was a class-based view and we have these pre-built behaviors but for the generic views it's telling that the generic views provided by rest framework allows you to quickly build API views that map closely to your database models so we're using generic views we will not write a lot of codes and also it's telling that if the generic view wheels don't suite the need of your API you can drop down to using the regular API views so now let me just show you an example and at how it's working so here the first thing we need we are going to also from rest framework we are going to import a generics so from rest framework import generics also from from rest framework import to make sense ok now the first thing we need let me just create in here generic API a class that's called for example generic generic epi view and now we can add generics dot generic view API view and also make sense so there are different make sense that you can use for example for a listing of your data can use less a list model make sense also we have create model make sense destroy model make sense retrieve and upgrade we will see at these make sense so let me just click on list model make sense okay so we are going to list our data now it's simple that you can just in here so the first thing we need we are going to specifically specify this réaliser class so it is article serializer and after this we are going to set our QC so the curie set is article dot objects dot all okay now we need to add our discreet method but for this time we don't need to write these all codes so you can simply write the get and it needs some parameters so cell we have solve our quest also you so for right now I am going to just give this and you can simply return so return self dot last and you can add your request so we are going to this list model mixing to list our data okay so now let me just just this this is just a view and let me test this and let me just copy this open my URLs and paste this in here and I need to create another pattern here and let me just copy and paste it in here so this time I want for example let me just change this to generic an article like this and it is Cherokee baby okay so now let me just refresh my page and see my API okay so now in here let me just add a generic also in here okay so now you can see we have generic epi in here and we have two data' and you can see we have just get emitted okay now you can see that just in two line of codes we have listed our all data so also you can use for example if you want to create you can also use so you can simply add Max and start create model mixing so create model mixing and let me write the post cell request and now you can return self dot create and you can add our question here so now let me refresh this so it right there you can see we don't have for posting data we don't have anything but if i refresh this [Music] okay now you can see I have four posts so let me just a new generate EPI vu and you can add the author further for example generic vu and you can add an email address and you can click on post then you can see we have another data and if I click on the get we have another data in here also you can use postman so you can send for example generic article sent request now you can see that we have three at the time here so this is the sixth one that we have created test so also you can add for example for updating data and let me just add in here make sense dot we have update model mixing and you can just simply write diff they put sell requests and also ID and return self dot a date we are going to add request and also we are going to add the ID in here and so let me just first check this in the postman okay so first let me copy one of these in here and let me put so now in the body okay so I want to just bring change to this let me just call generic API view updated updated and if I click on send so we have let me refresh this okay so you can see and here it's telling that expected view general keep a review to be called with your keyword argument named PK fix your URL conf or set that the lookup field attribute on the view correctly because we have used ID and we are receiving this error now at the top and here I'm going to add a lookup field so lookup field and add ID in here so let me save this and now I am waiting for the okay now it's refreshing reloading my data or my server and now I'm going to open the postman and send it again so again I have a problem okay also in here we are going to really need to check in here so let me just an ID in the gate so get none and let me just so F ID we are going to return so so dot retrieve retrieve and we need to pass there a question here and the else case we are going to do this and also we need to add this retrieve model mixin in here mix n dot retrieve model mixing okay so also let me just add and one more in here that's called def delete so they delete and return solved or destroy okay because we need to add destroy model mixing in here make sense don't destroy model mixin working our three self dot destroy and here you can add request and also ID okay now let me just also request an ID in here okay now we have a door this is a request ok so now let me just check this and it's working or not and also an important issue that because we are receiving error and our URLs dot pi so in here I need to add in the ID so because we have idea indeed okay anti-d so sorry I forgot to add this because by decision in the first I have received errors so now I'm going to use the postman and so first let me just click on the get and I want for example the so let me check that a fourth article okay so params 404 not found [Music] okay get also I have forgot to add okay now first let me just check in here okay so for example I want the for article okay now you can see this is the for article now let me use postman and article for $0.04 okay so now this is article four and I want to update this and let me put and you can go to the body body and in here let me copy this so I want to just give an update it in here so update it and click on sent so again we have a problem okay so we need to remove this ID I think because we have ID in here so sent so in here it's selling that email field may not be playing so let me just an email in here like this and now I'm sending sent okay now you can see we have 200 okay and now it is updated so if you see the for now you can see it's updated and if i refresh this and now it's updated in here you can also delete if you want so you can click on delete now the data is deleted [Music] in the previous part we have talked about generic API views in Django rest framework in this part we are going to talk about authentication so there are different authentication that you can use for example we have basic authentication session authentication and also talking on talking at indication so if you see now everyone can access to our API also if I go to for example six six data you can see I have the six data now I want to restrict this for this first we are going to see the basic owner authentication or Association authentication and after that we are going to see the token authentication and we will talk about the difference of these authentication so if you see Django rest framework documentation and in the authentication section so so first let me talk about the these authentication so the first the basic authentication now this authentication is schema uses HTTP basic authentication and it's signed against a user's username and also password according to the documentation of Django rest framework this authentication is generally only appropriate for testing purpose so if you want that your API be production and production level it's not recommended to use basic authentication but now but it's also telling that for example let me just come down and basic authentication such telling that if you use basic authentication and protection you must ensure that your API is only available our HTTP you should also ensure that your API client who will always request the username and password login so another one is the token authentication and according to Django rest framework documentation it is the authentication this authentication schema uses a simple token-based HTTP authentication schema and this talk and authentication is appropriate for client-server setups for example if you want to access to the API to mobile applications like Android iOS or you want to access through desktop native applications so you can use token authentication also we have session authentication so let me come down so now this authentication schema uses Jango default session back-end for authentication and also this session session authentication is appropriate for Ajax client that are using in the same session context as your website so now let's see the examples and the first thing I need so we need to add our classes for example our authentication classes and also permission classes so there are different ways that you can do the first way that you can add globally in your sitting start by so you can add that for example in here so let me just show you the documentation and so you can see that this is globally you could just copy this and you can add this basic authentication session authentication so if you want to add another authentication you can add in here so the second way is that you can use any reviews we are using this way so you can just create like this authentication classes and also permission classes and your views if you're using generic views or you are using API class based API views so if but if you are using a function based with decorators API view decorator you need to use like this so at identification classes so session authentication and basic education and this has their permission classes so we are using going to use this way okay now I have let me just open my views dogpile and in here the first thing I need I need to first import this in here and you can simply import from rest framework without authentication we are going to import session authentication and also basic authentication like this also we need to from rest framework at least you need to add one permission for your authorization or basic authentication so now we need to also import from permissions we are going to import as authenticated so as you can see we have s at main user is authenticated is authenticated originally we are going to use as authenticated like this okay now after this and our as we have created in here for example serialize our class over Curie set and here you can simply add authentication classes and you need to specify your authentication classes authentication Association it is a last session authentication and also basic authentication so now this class first check for the session authentication if there is a session authentication if there is not available a session authentication after that it will check the basic authentication so it's like first it prefers the session authentication and after that it will check for the basic authentication now I have said that also you need to add at least one class for a permission class for your authentication so let me add permission class and in here it's a list so is authenticated like this okay so right now if I save my this so now waiting for the server that it's reloading okay so now if I check my this for example API and I want to get the SEC's article and if I hit enter okay now let me just check okay so it's because it's showing me the data because I am logged in in my admin panel so if I log out okay so now if I check this you can see detailed authentication credentials were not provided let me open my postman and let me check it in here so let me just enter the URL and if I send request but now you can see authentication credentials were not provided now we have simply added a simple authentication if I want to seven article I cannot access to that because it is telling me because we have used in here so first the check for the session authentication after that it will check for the basic authentication so the basic authentication is that you can use your username password and also email you can access so let me just show you this now and the postman let me just go to headers and from the headers in here or authorization and and here let me choose basic art and and the username in here let me just add my username and also let me just add my password now if I send the request okay now you can see I have I have a sex fully access to the data and you can now access to another data for example the sex one you can see this is the six one meter and if I for example remove this and if I make it back in her it out from like this and if I send it again you will see that authentication credentials were not provided okay so this was a session and also basically on the kitchen now let me just show you the talk about talking authentication so I have said that the talkin authentication is useful when for example when an Android app or for example is a poor Native Client desktop applications want to access to the API so for that you cannot use session you need to use talking based authentication so for this the first thing you need because you you need to create a table for talking and the database because the token will be C saving the database for this you can open yours just setting dot PI and in your installed apps you need to add for example you need to add this rest framework dot auth token after this you need to migrate because ok so let me just create a new session and in here let me change directory to my project and python managed dot pi my great so now rest are no module name so I think I have mistakenly add let me just check the documentation that so for the token authentication so let me just copy this and paste this I think I forgot to add a comma in here so let me just yeah okay now let me just do it again okay now you can see we have our token and we have two models for the classes now if I check my so if I go to administer to it main so summer starting so we have an arrow so let me just run it again vitamin e start firing server so run server okay so now this is my admin and let me add my user and also password in here so and let me just login okay so now you can see we have another more table in here it's called token so let me just create a token I'm doing this in this video manually so let me add token and right now I have just one user and I want to create the token for this user so now the token is created now in our now in the views dot pi let me just comment this out on occasion class this time you can add the token in here but let me just for me that I show you so authentication classes and this time we need to import the token authentication so talking authentication so talking authentication token authentication and in here we are going to talk in authentication and we need the permission classes so now let me just go to postman this time and let me just send you will see obviously obviously that we are going to receive a and valid or authentication credentials were not provided now we have created hours talking now and in the headers so in here at the top in the headers and the key let me add an authorization and for the volume talking and I'm going to just copy the token that I have created in here so let me just copy this token and paste it in here so now let me just okay now you can see if I send it again now you can see now your ex valid and this is because the token that using the token and if I go to for example seven article and send you can see now it's valid and if I remove this again and if I send okay now you can see authentication credentials were not provided and if I check it again and if I send this so I have received my data so this was the basics of Django rest framework authentication in the previous part we have talked about a Django rest framework authentication in this part we are going to talk about these sets and also routers so January's framework allows you to combine the logic for a set of related views in a single class that we can call it view sets so a view set class is simply a type of class-based view that does not provide any method handlers such as get or post so if you see in our that we have worked in one of our videos in our API view so it does not the view sets does not provide as ticket and post so it provides instead it provides such as for example a stand also create a retrieve or update it's this kind of operations so now let me first create an example and I will say that there are different models of view sets that you can use so for example let me just create a simple example of this so this time I am going to use article view set so we use it also we need to import this so from rest framework import view sets okay so now after this we have created our article view set now the should inherit from the view set sorry so from the view sets dot now you can see we have different view sets we have model views that we have generic view sets so this time we are going to use the simplest view set that is called view set so now if you use this type of view set then you need to write all of these functionality for example if we have get post or if so let me just show you the functionality that what functionality we can do so for the last retrieve or if we come down for example last create retrieve update partial update and destroy so we need to write all of these functionality by ourselves so if we you are using view sets dot views so now let me just create this example that okay now let me just first create the list because I want to list my data and it takes request okay so after this now I'm going to just copy in this video we will have some more copy and paste I'm going to just copy this data and paste it in my this list so just simply I'm going to get the objects after that I want to serialize the data because I'm using a queue reset I need to pass manatee through also after that I'm going to again response the serialize Earth or data so now this was for the list also you can create for the retrieve so retrieve and solve request okay so first let me do for post and after that full retrieve let me just so retrievers when you are going to update the data you need to add a receive metal so let me just copy this post and because the mechanism has changed we are not going to use a post we need to use diff create so now it takes parameter like this okay so now this is our data we have added our data to the serializer and we have good data to request and after that we have checked that if the serializers valid we are going to save and we return a response of our data with the status code of 201 it means 201 it means that our article has created and if it's not valid we are going to receive as realizer to the universe also let me just do for our retrieve for update I'll show you just first I am going to do this three and after that for update I will show you so because now let me give request for a treat because it is related to the update so when you update and when again you need to retrieve the data so for that you need to add retrieve so I have messy spelling like this okay so also because you need to now add a PK so PK none like this so if you're using ID you need to add lookup field for your ID so let me just I've showed you to lookup fields and the previous part let me just I'm use PK or primary key okay so now first we need to create a QV set because we are going to get the data so let me just copy these all and then let me just paste it in here okay so now I should change this to a Curie Satya Curie set like this okay now after this creating the Curie set now we need to get the data so for this also you need to so we need to get the object and also primary key so in here you need to from Django dot shortcuts import get object or 404 so you need to use this from Django shortcut okay so now we need to create the article so article and in here we need to use our get object or 404 and in here you need to pass the so it needs the Curie set and also it needs the primary key so piqué piqué piqué okay so now this is our retrieve also we need to return response okay now first we need to add the serializer and after that we need to return the response so we need to add these two so you like the art we don't need this money to true this time and we need to also I have changed this to article and just we return the response on the serializer data so this was for the retrieve and now let me just do another in here for creating the post so to create I need solve or I have done for create let me just check this I forgot yeah not need to this ok so now I have a created my DS methods and view sets now we need to bind these to the URL now if you see the documentation of Django rest framework and let me come down there are two ways that you can do so ok so this is the first way that you can use but it is not recommended to me at the second way is the recommended and best way is that you need to use routers so like this you can use a router so now let me just do this first let me just copy this and the URLs I need to import this okay so also I need to from rest framework taught routers import default router first we need to create the object of router so router and this is default router and after this I think we need to register the router so let me just check this router not register yeah so router toward register and add in the register first you need to add the prefix for example it should be article and after that we need to give our this article view setting here so and also you can add a base name so bees so bees name and let me just give the base name whatever you want but let me just give you the article like this okay so now we have registered our router routers now and a URL pattern now first we need to add yeah I have added include in here so we need to part I want it to be view sets view set but you can leave it blank there is no problem if you if you leave this blank you can access the URL just to article but if you give it like this view set first you need to after localhost you need to right view set and after that you need to write an article and we need to include the router dot URLs so now let me just save this and my server let me wait for the reloading of my server and okay now this is a correct and now let me just go to have you set remove this generic so we are going to defuse it and also we are going to the article yeah now you can see we have access to our data through view sets and if you check the EPI route and this is the apply route and if you click on this you can see we have this and it allows the get and post methods for right now we don't have update so now let me just post a daytime here and it should be let me just copy remove the ID because we want the ID now so you set view set data let me just post this okay now you can see I have ID 9 post and if I go to get you will see that I have I denied also you can use postman so let me hit this URL in the postman so I'm going to send the request okay now you can see we have these data in here okay so this was for creating a post and also getting post and also now we need to do for the retrieving so okay now for the retrieving it's also just simple for the retrieving not for the plating sorry so div update request and PK so PK - [Music] and I need to just copy from my pot yeah I need to just copy tennis and paste it in here okay so because we don't have this we need to get the ID from here so now let me just so that get so dot yourself article without objects dot get and piqué piqué so we are going to get the data and after that we need to serialize the data and we check for the serializer so now let me just create another usual URL for this so the basic URL let me just copy this spot and this time we need and and we need to add peeking so then I am waiting for the remoting of my server so yeah I don't have any problem I think if I go to for example article 5 okay so I have article 5 in here now let me just you can see I have pot now in here instead of you can do you can put this so let me just copy and paste I want to this is updated this is updated view set and let me just change this and let me click on put okay so now you can see it's changed this is updated view set and if I get my all data so that we just particle so now this has changed this has updated view set and if you had from postman so let me just of again five and let me just send this this is five let me just copy this this time I want put and and their body row let me just add this and this is postman updated let me just send this hit this URL and support that's media type oh sorry because we need to change this to JSON so because we are using JSON okay now you can see 200 okay and it is changed and if I check here I'll so let me refresh my on all the articles so now you can see it is post when I update it it's also up you can do delete I don't want to do this because now you can do so these are the actions last create retrieve update partial update and destroy in the previous part we have talked about view sets and routers and also we have saw the first type of view set so that's called view set so we have said in the previous part that if you're using this view set then you need to add all of these last create and also retrieve or delete data or update data so another kind of view set s that's called generic view scene so now just let me remove all of these so the generic are the generic view set is like our generic epi V you need to use that with the make sense so for example if you want to west model you need to add the West model make sense of update model mix N or retrieve model mixing so let me just show you this time I'm going to use a generic view set okay so for this you need to just we need to copy sub datas from the previous part okay so we need to just R at these serializer in here so let me just copy paste it in here for example I want to list my model so you can simply use make sense dot West model mixin okay just like this now you don't need to add anything more so for listing any track test now let me just see the - sir we'll use it ok now you can see I have listed my models but right now I don't have it's just for listing model and I have just get you can see it allows just get method mean here okay now let's see the another make sense like here for example this was West model mixing for example I want to create the data so let me just copy this and paste this in here it is a mixing dot create model and right now if you see we don't have pot or for example create article of posts so now if i refresh this okay and now you can see we have post get and post and if you see in here we can post the data so you can add the title so my title and you can add my author and you can add my email at gmail.com and you can post the data so I have the 10 ID and if I less the data I have another data in here so let me also add for example for that pitch mixing mixing let me just copy this and so I want to paste this in here okay now if I good for example I want to update this ten number so let me just go to ten so I'm waiting for the refreshing of that my server so probably I will receive a turn because yeah you can see method get not allowed because we need to also add retrieve so what update we need to add this Maxine dot retrieve so let me just copy and paste this in here and let me now again refresh this okay so now you can see and I can update this data so I can put this for example my new title my new author my new put okay now because it's updated my new title and if I see for example my article data there ten data is this data is updated so also let me just add another one that the delete so the destroyed makes sense so it we will complete this our this view set destroy morale mixin now let me just refresh this okay so now if I good for example ten you you will see that I have delete also in here so now you can see I haven't lead and I can delete this data from here so like this and now I don't have the stent data also you can use postman it's using the same so let me just get the article so you can use get and let me just sender a quest and params so you can see this 200 okay and we have read this data in here eight data in this part we are going to talk about model view set so we have tsar junior abuse it now what's model we've said if you see Django rest framework documentation is telling that the model view suit class inherits from generic API view and includes implementation for various actions by mixing in the behavior of the various mixin classes so it has by default a list retrieve create update partial plate and also destroy so now let me show you the example so this was the generic view sets now this time let me just remove all of these and okay now view set dot model views it just you need to do this and you need to add your suit eliezer class with your curious it now just simply now you can access to all these functionality like get post put delete just by adding these two lines of code just class and model view set in this so now let me just refresh this this time so if I go to the vertical so you can see I have my article I get post if you want you can post an article so this is the newest article and use alter and you can just leave an email address for example and you can post the data so we have the 11 ID and if I go to for example my 11 ID data you can see I have delete in here by default and also I have a pledge so you can update this so this is the newest article updated and updated and if you put and now you can see this is updated and if you see your article list our 11th ID is updated so this was modeled view set
Info
Channel: Parwiz Forogh
Views: 304,264
Rating: 4.9200726 out of 5
Keywords: Django REST Framework Full Course For Beginners, Build REST API With Django, Django, REST Framework, REST Framework Full Course, REST Framework Full Course For Beginners, Django REST, REST, django rest framework, django REST Framework for beginners, Build REST API, REST API, API, REST API With Django, API With Django, learn django rest framework
Id: B38aDwUpcFc
Channel Id: undefined
Length: 142min 23sec (8543 seconds)
Published: Fri Jan 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.