Django Rest Framework | DRF | GET, POST, PUT, PATCH, DELETE METHODS with POSTMAN #DRF #RESTFRAMEWORK

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so hello everyone how are you guys so in this video um we will see what is api and what is the rest framework and how to implement rest api in the jungle right so guys uh without delay we will start uh implementation of drf dejango rest framework so first of all uh i want to tell you one thing guys we have to know about what is api and what is raised so what is api api stands for application programming interface so api is an interface between two applications right so suppose we have we have one project and uh every project have two ends first one is a back end and second one is the front end so uh so who is responsible for communicating uh who is responsible for these two applications talk each other so api is responsible for talking this application each other it means api is an interface between two applications front end and backing right so now we will see what is risk rest rest wastes what is the risk and what stands what is meaning of rest stress stands for reproduction states transfer so it is just a one style architecture which defines some rules how to write a api and how to perform all api and maybe i have methods and all requests right right right so now uh we have to implement django django rest framework so now first of all we have to open one idol i have often pycharm you can go with the visual studio or any else anyone other other idols now so we have to create one project and activate our virtual machine and virtual environment so you can see here i have already already activated my virtual environment and now we have first of all we have to instructive install django by using pip command we can install django framework as you know uh every time when we create a new virtual environment that time we have to install django framework it's getting installing guys now second step we have to install django rest framework this is a library which is used to implement and uh uh performing all rest api in the jungle stream right guys so you can see here what i am going to install i have installed this angle rest framework and now enter it's installing guys it's install if you want to check uh these two library install or not you can go inside virtual environment and there is one directory inside when we our inside our virtual environment there is one library leave name folder and inside lib you can check here we have successfully installed you can see here guys the jamboree framework and here we stream yes guys this is a restrainer and uh our django stream it means we it means we here it means we have successfully installed our two libraries inside a virtual environment now close this and let's start our first project so just i will create one project biozoom django change directory then create one application django admin start app app one then next step open the demo project file and setting dot py we have to register our install application first application is our app one second one is a rest framework this is must guys it's important without this registration we cannot implement our rest framework in django right then control yes and close the setting dot py file then close this file now open app one our app one package and add two files first one will be serializer serializer file this serializer file is responsible for converting python data right python data into biostream data what is the buy stream data by stream data means which is responsible which is readable by uh every language it means a back-end and front-end json user message format which is used to store data and retrieve data and forwarding data from one end to another we can send data by using json so json is a cross site language right cross slides cross site compatible language so then one more folder we have to add one one more file it's a urls file now just start designing first one i will create one model employee model models dot model and pid equals to models dot model now guys model dot integer field we have to implement integer field e name goes to model start careful max length will be 50 then e cell models dot float field right guys then i will return string method string format self dot pid then self dot enum right now open serializer dot pi py file and import serializer class from redstone serializer class and import models which is model with our model is employ model we have to import it then create one class of serializer employee serializer employee serializer we have to inherit it from serializer dot serializer but i am going to inherit it from model serializer so no need to implement create an update method so i will go with model serializer class then create one class meta class in meta class model equals to what is model name employee then fields we have to add all fields right guys all fields underscore underscore all underscore underscore then ctrl s then move forward i will open views dot py right guys so in views that we have we have to import from the rest framework dot response response class from response model then from rest framework dot views will go with api view class api view class then from rest framework import status for our request of status of our request then we'll import from dot models import employee then from dot serializer we have to import serializer class right so what is employee serializer then uh inside this framework uh we will implement class base view not any uh junk uh function based view so we will go with class basis so first of all i will create one class employee class employ detail class i will inherit it from api view then we have to we have to implement some methods uh the first method for getting data we have get get method for post data we have post method then for updating data we have a put method it's a complete updation then we have patch method for only partial operation and last one is a delete method for delete the particular record right guys so we will go we will go with the or these all methods first first method will be gate method it's compulsory guys we have to write this great method instead of this gate method name we cannot write any other name so remember it it's so important so first one first method will be get method will pass request then we create one object we have to get all data so we will create one object and access the all data from our employee class employee dot objects dot all it's so or and fury guys you can you can search all over inquiries in jungle you can go with that so i'm going with all all record then i will create on serializer variable and access employees employee detail employee serializer we have to import employee serializer and inside this obj comma main equals to true it's very important because uh we are returning all records so many equals to this parameter is responsible for that so we are we have to return a response class what is important of this class this class is responsible for converting the all object data and python class data in json right this class is responsible so return serializer dot data comma status what is status status equals to status dot it's so okay 200 okay right then uh second method will be post then request then we have to access we have to create one serializer inside serializer we create a serialized employee serializer and pass the data data equals to request dot data right then we check if serializer dot is valid then serializer dot save right and return what we have written we have written response we will return serializer.data comma status what is status equal status our status will be our status will be created we are going to post data so it will return create the status of our request otherwise return return error data comma status equals to status dot bad request right guys so we have uh implemented two methods it's a gate method and second one is a post method it means we can post data and get data so for that we can check we can check by using url so you have to implement url so open urls.your file in f1 so first of all import django. urls path then from dot views import what we have to import we have to import employee detail class then create one list url pattern and inside this list first path but what is fourth part is our path is amp then employ detail as view so as you uh in in simple project we will will use just a method base right method base and function based view so no need that that time we don't need to write as you but inside uh rest api and rest framework so the india we have to write all method as view it means it's class and uh inside this class there is one method as view is method it will return or url right and then name will be emp so right then open our project level urls first one we have to set path or app one then include we have to import first of all import include function from uh junkrat urls and register apple dot urls right then save then now now first of all make migration we have to perform migration py manage dot py make migrations then perform my grid command it's important for database these two two commands it's compulsory they now run server it's running guys there is no error and just click on this url and just open what is url f1 road slash what is a applicable url it's so emp right yes guys you can see guys here uh we have we have successfully uh map our ui and you can see here guys uh when we perform get method it's showing a empty list yes guys you can see here it's empty list so now i want to add data it means i want to post data so here is the one there is one content content till you can add data in curly bracket then what was our fill tid a id then what is the e-i-d-e-i-d is one then e-name what is g-name e-name will be our employee name will be aj then e cell result will be 50 000 and i have to send data one second guys i will check our attributes and our fields yes right so i want to post data i will click on the post so you can see guys here we have successfully post our data by using api right guys so when we click on get data so uh all right it returns data in json format right guys you can see here uh id id in a m and selfie so now uh moving forward we have to update data and delete data so just return to the overviews dot py and create one more class employee info and inherit it from api view just get perform get method get method from and pass id because uh if we want to particular record so we have to pass id so it's it's important keep remember guys don't forget it so if there is no record inside table and in our database so we have to or we have to raise exception so we have to check uh if there is a exception or not so we will use try and accept block right guys so i will put the the code which will raise exception or there is chances of raising exception i will put it in try block so i will create an object and for that import employee dot object dot get method by using gate method we can get a particular record right guys so id equals to id will pass id question id and then accept block in exit block will raise error all right guys we'll accept and handle our error or raise error will handle our raise error right so return of employee dot does not exist does not exist error and return a message what will be the message message will be will create one dictionary and inside message will return not found error if record will not found and return response what will response response will be message comma status equals to status dot 404 http underscore four not four it's not found here right so if there is no error or any error so it will not execute a except block and move forward with line number 33 so it's a serializer and inside serializer will import access serializer and pass object data obj data and return what will be the written response let's respond in response serializer dot data comma status equals to status dot it return okay right so this is the method which is responsible for accessing a particular one record and in uh first class there is one method gate method uh this method is responsible for getting all record and now we have to implement second method it's a put method what is userput method put method is used for complete updation it means we can complete all we can update all record and we can implement all record it means access and update update all record all fields so we have to write request comma id so just same like that we will update uh all all fields from one record so there is chances of access there is chances of raising error if record is not found we will go with try and exit block so create object inside of try try block employ dot objects dot get method and id equals twice now android exception except block charging employee dot does not exist error does not exist and return message equals to employee dot more employee it's so messy so you create on dictionary and in key and value pair because why we are using your dictionary because it is written in json format so json format use dictionary right n value pair so not found error not found error now just return response what is response response will be message comma message comma then status equals to status dot 404 not otherwise it return a serializer class first of all create will see another class create object observator class and pass obj then we are going to update data so update will the data will be post it means we we are going to in insert data so we have to use data equals to request dot data right guys and check if serializer is valid so serializer is valid then save serializer and return response class serializer dot data comma status equals to state status dot http reset what is reset contain itself to not file right on either otherwise return response serializer dot error message and status will be status will be bad request it's a 400 right it's a put put method so what what is use of put method what method is used for complete updation of particular record right so moving forward one there is one more method name patch method so we will so nothing is different between this put a patch method code just to copy this all line of put method ctrl c and ctrl v here inside the second class and just change name instead of put it's a patch patch and uh just line number 61 there is a one object of employee serializer class just add one parameter there it will be the partial equals to true what is meaning of this partition partial updation means ah particular field known if we want to change only one field of employee suppose i want to change employee name so that time i can i will go with patch method it's a partial equal to true now and the last method is delete method so today delete like uh same as we will pass request then we'll add id then raise exception okay to an object employee dot objects dot gate method what is gate method i get method used for returning a particular record it return but it related an object right guys instead of q reset so handle exception employee dot does not exist record right i return message equals to message what is message message will be not found here then return this response message comma status equals to under status status will be http 4.4 and if there is a record found in record in when we send a request for delete record that time it no need to accept it will not execute exit block then execute object dot delete method this this method will be delete our all record and return response inside response will return one dictionary first parameter will be message what is message message will be deleted if we delete a record so it will return deleted and status what the status will be let us be status dot it's a no content when we delete record that time it the status of our request will be no content and control yes now guys i will tell you one api testing tool there is one one tool named postman so if you don't know about postman you can search and you can watch my one more video i have created one more video uh on postman the post manager tool which is used for or testing our app api is our api is working and working properly or our all api request working properly or not we can change so we can check all request so now i will tell you uh how to perform get method post method put patch and delete so first of all i will close all all these windows one second guys so i have so i have close all window and open for only one window what is our url our email is f1 forward slash emp right and then forward slash first of all we'll get data the same request you can see here guys uh it returned on the daytime in this environment you can see here json for my data so no need no need to change and our status or status of our api is 200 okay right guys so now i want to put it i want to add data one more data by using post method so first of all set here post method and inside body will add raw and in draw will add the field what is the field it's the second record it's a eid to comma you name a name will be vj then esl itself will be 55 000 right and send send request right guys just save data so you can see here we have successfully created and post data in our database by using post method in rest api so status is 201 created right so now uh now we'll get a particular data right guys we want to get particular data so i want to get only one report so i will get i will change request get get request and will add uh will return only one record so what is a our what is our id id is one so add in url bar id and forward slash after the emp url right and send request so something is wrong guys one second what what is error so it's so so we have oh so sorry guys so we have to add one more url so just add one more url in path in app level urls so open path and then emp forward slash in id then forward slash comma what is second class second class is amp info right just import it and add view and then now we'll send request open or postman and same request you can see guys here i have retrieved only one record by using get method so now i want to uh update it means i want to update record it's a put method bios input method we can update complete updates we can perform complete updation so now i want to update record 1 record 2 and in record 2 i will update instead of vj i will add sanjay right and then just change request it will be the put and same you can see guys here we have successfully uh perform our put method by using put method we have updated uh our a name equals to sanjay from vj right guys so now moving forward we have to implement we have to implement uh next method what is next method our next method is patch method what is user patch method patch method is used for partial may only one record of particular particular record it means suppose i want to change only one record i want to change each salary so i will uh delete all fields and just instead uh just write this same this cell will be so my result was 55 000 right so i will change it instead of 55 000 to 60 000 and send request so you can see guys so we have successfully uh implemented our patch method right guys so till now we have implemented uh get method then post method then put then patch and now last one is a delete method so now for a delete method uh we have to delete a particular record we can delete only one record at a time so for that uh i want to delete number two record so just just set path and just set url to record number two and change the request delete and now it will none and send data after send request now you can see guys what is the status of our our uh request the status of our request is deleted it means we have successfully deleted a particular record and thus the status of our request is it's a no not for no content right so guys i hope uh you have got everything uh so how to implement api and uh how to implement rest api in django framework and now i will break my server and so guys now it's over uh it's so we have implemented all methods so i hope uh you have enjoyed this video and uh i hope you have enjoyed this video uh then like my channel and subscribe my channel and like my video so thank you guys
Info
Channel: How to Code
Views: 9,256
Rating: undefined out of 5
Keywords:
Id: 0jR5UFsAHkU
Channel Id: undefined
Length: 35min 7sec (2107 seconds)
Published: Tue Mar 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.