Django Rest API CRUD - GET, POST, PUT and DELETE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome to core defection in this Python Django tutorial we will implement restful api cred operations with Postgres using web methods get post put and delete if you find this video helpful please thumbs up this video and for more hours and videos like this please please subscribe to this channel code affection for this app development I will be using Visual Studio code editor first of all we have to create the Django project inside this folder directory for that we have to open command prompt from this folder directory so here we have the shortcut hold shift then right-click then you may see the option open command window here in my case it is showing PowerShell instead of command prompt due to the latest windows 10 update I prefer command prompt instead of PowerShell so in my case I will use another shortcut inside this folder detector here just type CMD then hit enter so it will open the command prompt from the same folder directory here first for we have to provide the Python virtual environment so here it is we have to work on django env now let's create the project using this command Django - admin start project after that we have to provide a name for this project it will be something restful api correct hit enter as a result of the execution we can you should see a folder restful api here now let's now we get inside the folder restful api cred now in order to open this folder restful api cred in visual studio code editor you can do this court face period hit enter so here we have opened the visual studio code editor from the same folder directory restful api quad as a first step inside every Django project we have to create an application here so let's create the application inside this project so first of all we have to open atom now in order to create an application you can execute this command Python managed dot dy and start app then name the application here we will be dealing with employee details inside the application so I will name the application as employee API hip and off since we have created an application here we have to mention that inside this project file sitting start by here inside this file we have this array installed apps what we have to mention the newly created app name so I will do that here employee API now we have to treat this application as a Django restful framework API for that we have to install the package django restful framework so here is the command to install the package P I P install Django rest framework I have already installed this Django framework globally inside my system now we only have to mention that inside these installed apps are here so we just need to do this rest underscore framework so here we have the base configuration for Django restful api in order to demonstrate this api cred operations we will be using Postgres DB so first therefore in order to work with Postgres from a Django project we have to install this package P IP p psycho PG to hit end off I have already installed this package P psycho PG 2 inside my system if you are working with posters for the first time you have to install this package using this VIP installation command as a database management tool for post Christie B I have installed this PG admin so let me open that here so here we are inside the PG ad let's create a new database I will name this baby as restful api DB click on save there will be a small delay after that you can see the newly created DB here restful api DB now back to the project inside this file we have to provide the database connection details here so here we have the object database inside that we have the property default instead of SQLite we will be using PostgreSQL so we have to update that here post clear SQL after that we have to provide the name of the DB here so it would be restful api DB just below that we have to provide the credentials user and password it will be Postgres and after that we have the password it is also Postgres and finally we have to set the host since we have the database inside the same system we can set that as localhost now we have to do the DB migration inside every Django project you can see some default applications that you can see inside this array installed apps here inside these applications there are some models for user authentication authorization registration etc so first of all we will do the migration for those default models so in order to do the migration we have to execute this command Python manage dot py and we just need to execute this command migrate before executing this command make sure that you have saved this file settings dot pi now let's execute this after the command execution you can see the newly created Tablas so let's refresh this tab stored here inside that we have 10 tabs from default applications inside the project now for this restful api we have to create a model regarding employee details so that we can do inside the models inside the application employee API so we just need to do this class employee and we have to inherit this class from models so we can do this models dot model inside this class we have to add properties corresponding to each column we have to add inside the table so first of all we have full name now in order to set the data type of the column we can do this models dot calf field so inside this column full name we can add string data inside this instance constructor we have to provide the max length so inside this column maximum you can have hundred characters after that we have employee code for that we have this property EMP underscore code so models dot cap field and max length will be three and finally we have this property mobile let me copy this we can remove this comma at the end since we are just defining properties for this class here for mobile max length will be 15 now in order to create this employee tablet we have to do the migration again we can execute two commands make migrations the name of the application employee API sorry make migrations hit enter now we just need to execute this Python managed or py migrate in order to see the newly created table corresponding to the employee model cross here back to the PG admin just refresh this tabs node here so here we have the table employee to let me execute the select our statement currently inside this tab we don't have any row so you can see the columns corresponding to the model property that we have mentioned here full name employee code and mobile so you can see all of those columns here apart from that you can see the automatically created column ID as a primary key for the tablet it will start from one and incremented by one upon new record insertion it is managed by the Django Arum itself now let's deal with the API cred operations which is the meat of this tutorial we have cred so first of all we have C which is for create or insert or you can say add meaning with this create or insert or add operation we have to add a new record inside this employee table for that we will be using the work method get' after that we have retrieved meaning with this operation we have to retrieve or fetch all of the records which we have already inserted into the employee table in order to do this operation we also use the get method after retrieve we have update update or you can say edit operation meaning with update or edit operation we are going to update a record using its primary key in order to do this operation we can make use of the web method put sorry my mistake for this create or insert operation we have to make the post request instead of get okay and finally here we have the delete operation or remove operation to delete a record we have to use the web method delete okay so these are the four web methods associated with each great operations so a less look how to implement these get post put and delete webmasters inside a Django rest framework as a next step we have to define serializes inside this application for that i will create a new python file here serializable dot py so when we walk Viv's api the situation will be something like this it will have an api and we will have an external application which has the capability to work with the api it will be a mobile app or it might be a web app or whatever the app or system or the technology which has the ability to communicate with the API here so inside the system there is a two-way communication of response and request so in most of the case this data flow will be either in JSON or XML format inside this application we will be dealing with JSON data okay so before sending data from API to this external app we have to convert the Python representation of data into JSON when we received JSON from this external app into the API we will convert the JSON into Python representation of data so in order to do this conversion from Python representation it in most of the case it would be Python query set and we have to convert that into JSON and JSON to Python representation so back and forth so serialize this is used to define how do we want to do this conversion of data so let's do that here first of all we have to import corresponding classes so first of all we have to import from this press underscore framework and here we have to import serializers and after that we have to import this model class employee also we can do this from models we have to import employee model class now let's create the class employee serialize them and we have to inherit the parent class for serializer here she realizes dot model serializer and here we have to configure how do we want to do the conversion so that we can use this meta class here first of all we have to provide the corresponding model class which is employee inside this model class we have these much properties full name EMP code m'appelle apart from that we have the primary column ID so using this field property here we can mention what are the columns we want to include inside this serialize them if you want to include all of those columns you just have to provide this underscore underscore underscore underscore if you need few of those properties you can mention that inside this double here like ID then full name etc in this case we have to include all of those columns so I will use this underscore underscore underscore underscore now we have to define an employee view set clause first of all I will define the clause and after that we can discuss the need of this music class so first of all we will create a file view sets dot PI file first of all we have to add the corresponding impulse admin from rest framework we have to import view sets beneath that we have to import model classes and then we have to import serializers now let's define the class employee view set and we have to import this class from you sets dot model view set now inside this class we have to set the query set coral set is equal to is equal to models dot employee model from that we can access these objects and we will call this function or after that we have to mention the serializer for this class which is serializers dot employees serializer so here we have the definition for employee view set class here we have retrieve all of the employer code and assigned into this query set here and with this serialize the class initialization we are telling we have to use this employee serializer for the json conversion for this employee view set by defining this view set for employee it will entirely create few classes like list then retrieve then create then it will create update function and destroy so in brief this employee view set class will create these classes for various web methods like our get post put and delete this list function will be used to retrieve all of the records which we have inside the employee table in order to retrieve a single record with given ID this retrieve function will be used okay then in order to create a new record this function will be invoked and for update operation this function will be invoked and in order to delete a record this destroy function will be involved so that's all about the brief description for this view set now we have to map URLs into these view set functions inside the application so let's look how to do configuration for routing inside this application for that I will create a new file inside this project folder here router dot py inside this file for stuffer we will import all of these view sets from our application folder so here it is from employee name of the which is the name of the application here we are inside the project folder here now we have to access this view set PI file inside this application folder so first of all we have to provide the namespace all the name of the app inside that we have this view set from that we have to import employee view set and we have to import routers from rest framework so here it is res framework and we have to import routers this routing configuration is also made simple by Django framework we just have to do this first before we will define this property router and we will initialize that with this default router function default router and after that we can register the routes register function can be called from this router variable and first of all we have to give the prefix employee and then we can provide the employee view set here we can discuss what happens inside this routing configuration here once I map this to the base you are a spy file inside this project folder here inside this you can already see a mapping URL mapping for admin applications just beneath that we can add this path and here we have the base route API forward slash and all of the requests which starts with API for slash we have to handle that inside this router dot py file here for that first of all we will import this include function also and we can do this include and here we have to import this router which we have just created so from dot router file we have to import route of variable which we have created and we can pass that here inside this router object we will have a function url's it will return all of the URLs for this API so back to the router PI file here so basically we will have the base URL something like localhost localhost then our port number something 3,000 then forward slash and inside this urs here we have the base path as API so API so all of the requests which starts with this format will be handled inside this router dot PI file that is what we have done here so after calling this function register inside this router variable will have a function URL that is what we have called here so it will return all of the path for this rest api will be of the type get then post then update then delete so these are the basic web methods inside a restful api so all of these work methods are mapped to corresponding view set functions which we have mentioned before so this gate function will be mapped either to lease function or retrieve function okay for this lease function we have to make the request like this forward slash employee so this URL prefix is mentioned inside this register function as a force parameter so if the request is like this this list function will be mapped so all of the records from employee will be returned in order to cause this retrieve function here we are trying to get details of a single to code so we have to pass the ID so in this case this retrieve function will be called both of these functions will be handled get requests after that we have the post web method for creating a new record in order to update a record this update function inside this view set function will be invoked and finally we have the delete operation so all of these webmasters are mapped to their respective functions inside this view sit now let me save all of these modifications now let's run this application using this command Python managed or py rent server so here we have successfully started the server in order to open this inside the web browser hold ctrl and click on this URL here and here we have the base route API for slash employee I think we have a problem inside the serialize ER let's check that here instead of fields we have to use fields save back to the app reload this boo so here we have maybe get requests usually it should retain all of the records inside the employee tablet currently you can see that inside this tablet we don't have any record so that is why we can see this empty array here in order to insert a new record we have to make a post because for that we need air for so that is why we can see the form here so let me insert a new record something now Fiona green employee code will be something FG then mobile number will be something 2 / 5 2 / 5 1 2 3 4 let's make the boss request so this page is refreshed as a result of that you can see the newly inserted record we can see the same if you execute this select statement inside the DB here it is so this interface is for testing purpose of the API in most of the case we will make this request from an outside application it might be a mobile app or a website whatever we have to make that from an outside application so let's look how to do that from an outside application in most of the case in order to test an API we will use postman so here is my postman now let's make the get request to retrieve all of the records so first of all I will make the get request and we have to provide the corresponding URL I will copy that from here I will paste that here send here that's it you can see the status code 200 okay and here we have the response it is an array containing objects of the type employee now let's take remaining web methods first of all I will make the post request so I will just duplicate this tab here we have to make the post request the URL will be similar make sure to add this forward slash here we have to set the header here so first of all we have to mention the type of the data whether it is decent or examine so first of all we have content type and we can set that here application for slash to JSON now inside this body part we have to send the our JSON data so you can select this draw here and here we have selected the JSON and we can pass the JSON object here first of all we have full name it will be something John Smith then we have EMP code it will be something j/s and finally we have mobile it will be something to profile triple five now let's make this post request send boom so here we have the status code two not one created and here we have the response this on object containing details of new employee with the corresponding primary key ID value as two now let's make this get request again it should return all of the records currently we have two employee records now if you want to produce a single specific record with a given ID you can use the get request like this let me duplicate this we just have to provide the ID of this employee /soe here we have the employee details of the ID 1 for employee - you can do this now if you want to update a record you have to make the put request so let me duplicate this yes now I remember here we have a typo instead of update transponding work method will be put came back to the postman now let's make the put request so if you want to update this employee record with given ID - we have to provide the URL like this and for this update operation we are going to send a JSON object so we have to set the content type as JSON and inside this body we can provide the corresponding object here I will just add few astrick marks along with the full name here now let's make the request something went wrong that is why you can see if this status 500 internal server error yes don't forget to add the forward slash at the end you can ignore this forward slash for because of attack get now let's make this port request that's it you can see the updated record you can see the same if you make this get request again you see here we have the updated value of employee name let's make the a delete operation so let me duplicate this tab here and this is for delete operation so I will make this web method delete and URL will be something like this employee ID with - will be deleted let's make this delete request we have the little Yuriko successfully that is why you can see this status - not for no content let's check that here see we have deleted the employee with ID - so that's all about the Django rest framework or abbreviated as DRF cred operations with post less TB we have discussed all of the purposes get put post until I hope you won't need this demonstration project source code even though I have given the corresponding github repository link in video description if you found this video helpful please thumbs up this video and for more our some videos like this please please subscribe to this channel Cod affection please share this video with your friends and colleagues so that they can benefit from this have a nice day bye
Info
Channel: CodAffection
Views: 62,927
Rating: 4.8760195 out of 5
Keywords: django rest framework crud api, django rest framework post, django restful api, django rest framework get, post, put and delete, django rest api with postgresql, DRF crud with postgres, python restful api crud, python api GET, POST, PUT and DELETE, routing in django api, CodAffection
Id: 1k0fRG098cU
Channel Id: undefined
Length: 28min 29sec (1709 seconds)
Published: Mon Nov 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.