Getting Started with Django REST Framework | FREE Full Course | SCALER

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is abhijeet and i'm a software engineer and i have a quiet experience in developing some python application from ground zero and welcome to the complete tutorial on django this framework in this tutorial we will be seeing a lot of things about django zest framework first of all we will start with what is an api why do we use a framework called django's rest framework when we already have a framework called django okay and we will start with a very basic concept about django zest framework that how what is an api decorator what are api views and some of the advanced topic of django zest framework like how we can do authentication in django's this framework and what are some model view set how we can do pagination in django and also we will be seeing different methods in an api what we can do like we will be discussing about get method when do we use really get method post method patch port delete and all of the advanced topic which you need to become a chango rest framework developer we will be seeing in this particular video so let's get started so yeah let's start and in this video we will see like we when we had a framework called django then why are we using a framework called zhang zest framework and uh like django this framework is a big framework but uh if we look about django this framework that is basically a library so let's start what are rest apis and why views django test remember so let's take an example where let's suppose we had an e-commerce website and that website was running very well and a lot of customers like were buying products and like our sales was at an insane level so like we people thought that instead of running that particular website let's start with one more thing that let's create an android application okay so like now we have two things one is like a website and now we have uh our android application or you can say mobile application both on android and ios but like the problem is when you use django you know like you are gonna pass html template let's suppose we had a like product page and on that product page we were passing like a product.html and on using jango context we were passing the data from the our database but we cannot do that thing on our mobile application or you can say any application so like now we are in trouble so this is where rest apis comes into picture and this is the place where django this framework also comes into the picture so like to interact our android application to our backend service or you can say our application we need something where we can pass the data where we can get the data from the android application and where we can show the data like if we want to like show some products if you want to show some user details like what orders he has made and some other information so we can use apis to interact with application between our backend server and android application so that interaction will be uh happen with the help of rest apis okay so in rest tps we basically do what we do is we pass data in in the form of json format and json stand for javascript object notation and we will see so what we do is we created our django rest framework we use django this framework and we converted all our like you can say html templates where we are rendering html remember we use rest apis or you can say djangos framework functions or you can say classes to create our apis so this is a place where django this framework can help you and basically we can create all other apis in django also but the problem is we can we cannot like there are already classes built in on django's rest framework which we can use let's suppose we are like we want to convert our query set into json or then we don't have any kind of mechanism in django but we have a mechanism on using django's s framework that is called serializer so that all the part of django this framework we will see in the later part of the video so let's suppose now now this is a place where we we understand like where we can use django this framework so we we created apis and all the apis was exposed to our website or you can say application and also you know like all these modern application like when when we are creating some modern application we don't like rely on only html we use some sort of like a front-end framework called react angular or view it's depend on choice so all these framework don't directly use html they use some sort of apis to get the data to pull the data to post the data and to show the data so this is a this is also a place where we can use our rest apis to interact with our front-end application and uh this is how json will look like let's suppose we had like or this and it's a you can see it's a kind of a list or you can say add it and then that either we had some object or you can say dict in terms of python and we have a like key value pair like order number this date when that order was ordered and uh we have a track tracking number customer id customer and and in that like you can say dick or you can say object we all we again have a list or you can say are they so this is what jason or you can say javascript object notation will look like and now i think you are pretty clear that why we are going to use django zest flavor and what's the use of django this framework so let's go and uh the installation part of django rest framework where we will use and install django's framework so this is a like official website of django's rest framework and we are going to use the this website for the entire video and uh let's start by installing django this framework and how we can add django's framework first of all we need to create a virtual environment and it's like it's a kind of a python thing so first of all let's install like let's create a virtual environment and let's name this at env and activate this virtual env okay uh now we are going to install django because like uh to use django this framework we use we first install django and then we install django dress framework okay so django is installing we get we are using like 4.0.4 version of django this framework and now let's clear the screen and let's like let's create a django application so type django admin start project and we will name the project as code okay and now let's go into the code and let's install django this framework so to install django this framework you can see like it's uh pep installed django zest framework so let's do that thing we've installed django this framework and now you can see we are successfully installed with django this framework so let's open this particular directory in the vs code so we are also going to use vs code for the entire project and this is this thing so first of all let's create and you can say app which will help you to like i think like you basically know about what are apps in django and if you don't know like you can do watch that particular video or which is on the scale channel so let's start by creating an app so now let's create an app which will be python manage dot py start app and the name of the app will be home okay so now our home app is now created let's open it in vs code and let's add what you can say res framework in this so we have already like installed the rest framework to to use the functionalities of this framework what we do is we add this framework in our installed app so let's do that particular thing open let's open settings.py and let's add first of all let's add register our home app and after that we can add this framework as it is okay so like right now we are good to go with this framework and we can create our apis we can consume our apis and let's start with api view so what is an api view in django this framework so basically what api view really means actually when you you have a functions and like you already write some functions in django and that function are responsible for rendering out html template and the data which you pass in the context but in api we don't like deal with html we directly like dump the json response and our link front and consume that particular api so api view is and like or you can say let me first of all open that thing so api view is a kind of a decorator which converts your existing django function or you can say python function into an api view function okay uh as you like already are familiar with the water decorators in python this is totally same it modifies the function behavior that now your function is capable of handling all the api's thing okay so let's do that particular thing we don't need like render in this so we can remove this but that particular thing and the like again one more thing i want to tell you that is very important uh when we are dealing with apis we have like some sort of method which we use to create or update or get data and those like every method is responsible for handling its own functionality so let's suppose we when we want to get some data from the database or get some data from the server we basically use get api okay and you will like see that particular thing in the later part of the video but i want to tell you that that it's very important okay and then it comes the post api so when you want to save some data and or if you want to send some data to the server then it comes post api and if you want to like update the data then it comes we have like two apis for updating uh two request method for updating the data first one is put and next one is patch we will see like in the later part of the video and let's suppose you want to delete some data we have like delete request method okay so all these methods are responsible for handling all their functionality and we use this method when you want to like get the data we use get method and we when we want to post when you want to save the data when you want to update the data views put a patch when you want to delete the data we use delete method okay so let's create and function so let's suppose right now with our function name is home or let's take it as index okay and now like it will definitely take a request parameter because we are dealing with django and now it won't written any kind of html so let's suppose we want to like written some json response like i hope that you are familiar with jason respond so let's suppose we want to familiar with courses okay so a courses has a dict okay so so let's suppose the course name is you can say python and and python what you will learn we have like something this uh learn a key okay and in learn key we will have a lot of things like you will learn flask you will learn django and you will learn tornado okay these are the things and you will also learn fast api okay we have these kind of thing let's correct the name of the python and you have a course provider so the course provider is scalar okay so these are the like three and we want to return this data and in the front end so we can use response instead of render and we can like paste this and now we want to like as you know we are getting the data so we are we are using the you can say get method so and we will use api decorator and in api decorator you can pass what are the methods which this particular function accepts so right now we are only accepting get method so we will apply a list of method so let's suppose this method was able to accept post also so we can type comma post and if put also so we can add a put method so now this function can handle get post and put methods okay so but now we are like only focusing on get method how actually api view works so we will like uh fix this thing so we will start to get metered okay now what we need to do is we can create a api folder so which will help us to like add all the api routes over here okay so like as you know like when we have let's suppose we have a domain of abc dot com okay now on abc.com our content will like go live and but all our api will be on api slash this thing so like a lot of apis you have seen have like have a prefix of api okay and all these apis start from here so you can see so our api will be interesting api slash index so to that to do that particular thing we have create an api folder and we can create a render init method which will help python to do this directory okay and after that we can have a urls dot py okay now we can import the function from chrome dot from home dot views let's import all or you can say let's import index okay that will that's more good okay now we need to affix the url thing so we can just copy that particular thing from here and let's paste it over here so on our route is what index and we can add it to our index over here okay uh i hope like you people are now good to go and now let's register this urls and do it over here so let's add path that will start from api and all our api will come over here let's import include api dot urls okay now let's include so let's test this api first of all let's boot up our server pipeline by typing python manage.piva and first of all we need to like make migration or you can say migrate because uh like django that's framework has some tables called session and other things so let's first of all might create and now we are python manage dot py run server okay now let's go over here and hit this api slash index so now you can see we have we are now successfully able to get the data and as you can see methods are which the the method which which we have called that's called a get method and the content type is in json uh this is a django s streamer kind of ui but we won't be using this entirely because we have something to test our api we have some some tools that we are going to use entire the video and the name of that tool is postman so let's install postman so you can go on postman website and install that particular thing you can install it from here maybe from here okay but i have already installed so i won't be doing that particular thing so let's go on postman so okay and uh now we can close all the things which we don't really need okay and let's now paste the route so we can copy that particular right route from here and hit this thing uh like now when you want to like send some post method you can toggle it from here let's suppose it's we are sending a post method and django server will throw an error that method post is totally not allowed okay and if you do for put that is not also allowed so if we will add that particular method over here post then it will definitely work but how would we know that this the method which is coming from the front end that is it's a post method or it's a get method or it's something else how we can know that particular thing so to know that particular thing we basically have we can check it we can add if else statement so if request dot method equal equal to post so let's suppose if the request method is post or you can say first of all we are checking we are going to checking check it for get so let's suppose if request was met at this kit so we can return this data and we can check like else alif or you can say llif lf request dot method equal equal to what post then we can do then we can return some other response like return response return response and we can return some other data so let's suppose this time we are returning some other data what you can do is we can return this thing as same but we will print something different okay so let's suppose we are hitting some get data so we can call this you you hit a get method okay you hit a get method and if we are doing this we can just copy that thing and paste it over here you hit a post method and we can just written same response from here as well okay now you can like see let's test this particular thing so first of all like we are getting hitting the get the get method so you can see you hit a post method basically okay sorry we have like hit the post method so now if we will toggle this method to the get method and we will hit send so now you can see like we it it's showing in like it's it's printing like you are you hit a gate method okay and you can take test for other method as well so let's suppose we want to go put method you can check check if request documented equal to put and you need to allow that method over here also if you will not allow that method it won't be getting in this like in inside this function okay so now you are like good to go with how actually we like uh we can create some apis okay so now it's time for something different so like now it's time for like now you are good to go how we can do get method get post put patch delete and all these stuff you can add it over here let's suppose i want to and like let's let me show one more example that how you can put okay so let's put and lf request dot method equal equal to what we can just return the same thing and we can just put change it over here okay let's do this we hit a put method over here now you hit a put method okay now you are good to go now let's uh jump into like serializer what are what really serializes means so like let me explain to you serializer like uh first of all when you like let's suppose we have a model called person and we want to get all the person which are on the table so our orm sentence will look something like this person dot objects dot all and like it will written all the person let's suppose we we have like three person or four person so it will written one two and three and four and as you know that the data type of this particular thing will be query set it will be a query set so we cannot expose our query set in the front end so it should be in a json format so to do that particular thing we use serializer so what serializer really means you pass some data and it converts into a json response or you can say json so that really serializer means uh like it helps you to convert all your query set all your data or your audio like django queries or you can say the data which you get from the django queries to in a json or you can say javascript object notation format it helps you in in that way and vice versa if you want to save some data it directly like convert that json response into a kodi set and save into a database so basically serializer means a class which helps you to serialize the data in the form from the query set to json response and vice versa and like we have some sort of you can say serializer uh in django zest framework uh or like the most used serializer is model view model serializer then we have uh like uh the normal serializer we have hyperlinks here as a less serializer base level but majority of time we only use this particular serializer class on this model serializer it like it they are like very very widely used when you create when you read some like a lot of code on github or in in company most most probably there are chances you will be using this serializer class and this model realizer so first of all let's create a model okay let's go over here and type let's create a class of person models dot model and let's first of all name this so it will be models dot car field on let's add max length of 100 and it should be a it has it has a page so it will be a integer field integer field okay and now let's create a class that's called serializer and first of all we are going to see that an example of model c lesson then we will see about what is you can say uh basic stabilizer okay see lizards dot p why okay so like before jumping into models i just skip one thing that was how we can accept the posted data from the front end let's suppose i want to send some data from here to the back end then how we can do that i just skipped that particular thing so let's just uh cover that topic as well so let's suppose uh like views so let's suppose we are in a pose method and we want to get the data from the front end so let's suppose i want to pass something uh okay let's suppose we can do this from the uh jason jason sorry and we want like send some data let's suppose i want to send name of the person and the name of the person is abhijit and the age so let's suppose the age is 25 okay so we want to send this particular data to the end server then how we can do that so right now we we can toggle the method uh like this thing we already know we can like toggle the method and we can send that whole way but how we can accept that data over here okay that's a question so we can do request dot data once we do this particular thing data is equal to request.data we are able to get the data which is passed in the form of json from the front end so let me show you with an example let me just print this particular data print data uh let's add some like special character over here so you can like basically see what's happening okay uh okay now let's hit this particular route and now you can see like we got the data this is the like features of a feature of jungle this framework like you just type this thing request.data so to if you want to access some data which is coming from the in the form of post request in the form of but patch you can do this particular thing okay and one more thing when you want to get some data from the front end uh in get requests you don't actually do this particular thing and that particular thing we use you can say query parameter you can say query string so let's suppose i want to add something in like if you have use some uh like e-commerce website like flipkart or amazon or any hotel searching apa there are a lot of chances that you have seen already seen this particular thing uh if you have work on django project then there are there are probably chances that you have also seen this particular thing so you pass this thing over here search is equal to abhijit okay so it will you can grab that grab this particular thing this only works like this thing you only use and get requests so if you want to do this so you can do request dot get dot get dot get and you just name the string so the name was search sorry not name search okay search okay let's hit so let's suppose you are doing a get request and you just send it over here you just got that data abijit and let's suppose i want to update it to rahul and you can get that rahul also and if you want to get the data from the like in the po in the from post like let's put patch delete you can do this particular thing request dot data let's toggle this okay i don't know like server is booting up so let's set now you can do this thing and if you want to get that like if you want to get a specific key so you can do this thing data dot name uh like in the form when you like do in the dictionary or you can say other things okay rpg and if you want to get the age these things actually work okay now let's go on the serializer part let's import model serializer so let's import serializer so first let's write from rest framework import sterilizer okay and let's name the serializer class so class will be like people serializer okay and we are going to pass model serializer class over here model serial and like to actually know which model to serialize we need a class one more class that is called inner class and the name of the class is meta so we need meta and in meta we add a model field which automatically knows that this realizer class is linked to that particular model so let's import that model also from dot models import person okay and let's paste it over here and also we need to add one more field over in the meta class that is what field we need to say the lies so let's suppose we want to serialize only two fields so we can type like fields over here fie lds and we can like add a list where we can pass two fields so let's suppose this model has name and h field so we can type name and age this and let's add in a string okay and let's suppose it this model has got a lot of other fields so we can type a like let's suppose the name of the field was a then we have b so we can type this also so let's suppose this we want to serialize all the fields which are on the model class we can type all also okay so uh also there is an example like there uh let's suppose there are ten of field in the in this particular person class and we want to exclude those two fields only we want to add all the fields but we want to exclude the two fields so we can we have a field you can say we have a like attribute called exclude which let's suppose which helps you to exclude the fields so let's suppose we want to exclude the name field we can type name we want to exclude the age field we can type age also so it will not serialize that field also so we will see an example that what it really means so let's suppose right now we are we want to select all the feeds which are on the you can say model so also we are like model is ready so let's create an api which will like help help us to get all the people and which will again help us to create the people okay so let's name this function as def people and let's pass request parameter okay and uh like first of all we need to add two methods the this particular a like function will be have able to handle these you can say two method the first one will be a get and the next one will be a post method so let's check if the method is get okay so we need to like get all the data from the database so peoples or you can say objects okay obj's now let's type person okay sorry the name of the like model is person objects dot all and we can again change this over here person okay so we have like we have like written a query that which which will fetch all the person from the database but we are going to see the license so serializer is equal to persons first of all we need to import from home dot serializer import personalizer okay and we want to pass this thing over here and as you know like when we type all it returns a query set so we will get one two three four five six as many as objects so we need to pass many also so if data was only one we don't need to pass many but if the data is like above if the length of the object is above then more than one so we need to pass manual many equal to two and we are gonna return response serializer dot data okay if this is a get get method and now let's do it for the like post method else so first of all we're gonna get the data as you know like when we want to get the data from the front end we do request.data request dot data also that we need to like convert into a query set so we type p pulse realizer and we type theta is equal to data okay and then we check if the like if the serializer is valid if like if if the data like let's suppose we pass the data only name and this particular model needs age as well so the data is not valid we can check like we like to save that particular thing in the database we need both the field name and h so it will check whether this data is serialized or not whether this data is validated or not so to check we can type if serializer dot is valid if the serializer returns true we can save this particular method with the using of save keyword or you can save method and we can return response of serializer dot data otherwise like if something went wrong we can return the error that these were the errors which we encountered when we were saving that data so we can do serializer dot errors okay so now like i think you are able to get this is our api for handling get and post data so let's add it into the url dot py so path which will be person okay so we need to import it as well so now let's check and let's clear the console python manage dot p by run server okay and now let's go to this api and type person and let's hit so right now uh like this data has nothing when you're getting get request name is required what is happening get okay let's see views request we need to add request dot method equal equal to okay okay so let's do this so right now this data like this all up uh you can say database is empty so it's returning uh empty response let me like bigger the font first so i think like now the font is totally visible so when we hit get on this particular api so right now this particular table doesn't contains any data so it's returning an empty list so let's do uh you can say post method and create something so let's suppose we pass a empty response so right now it's saying it's throwing an error that name and edges are required so let's suppose we remove we add this thing we only want to serialize name field so now you can see it will return only one other that is name is required name is required and if we remove it by age so now you can see it requires only age field i think like now you are able to get what this actually means okay so let's back to the all field okay let's send it and now you can see name and h both are required so let's give it a name okay so name and name is what let's suppose abhijit and h is h is something around 24. let's hit this so right now like it written a success response that status is okay and we are able to successfully save the data and let's hit our gate api okay so let's now you can see we are able to get the data let's create some more people so the next people name will be let's suppose xyz and let's get one more people abc with the age of 35 let's do this and now let's do the get api hit the get api so now you are able to see we are able to successfully get all the data from the database so id is this yes and this okay okay let's close this okay now send okay now you are able to get so now let's see uh how we can update the data because we are able to get the data we are able to create the data but we are not able to update the data let's suppose we want to update the age of abiji to 36 35 40 and we want to delete this thing also so now we are going to see the difference between put and patch apis and how we can add our delete api as well so like for this particular thing we are going to create one more you can say function that will help you out otherwise we can add in here also so first of all let's create a put method and then we create a patch method so now we need to check l if request dot method equal equal to if the method is post we are gonna hit run this particular code if the method is good we are gonna run this particular code and if the method is what if request dot method equal equal to put we are going to run this method so first of all we need to grab the data so we can do this thing and we want to serialize the data okay and now we are gonna return the same data so first of all let me tell you the difference between put and patch method both the method are responsible for updating the data but there is a minor difference between put and patch method so first of all let me implement that method and then you will be able to understand that what's the difference put and patch okay so basically input method we do both as we know like both the methods are responsible for updating the data but the problem with put in patches patch is able to like it supports partial update and put doesn't support partial update what it actually means so let's suppose we want to update this particular model uh with the you can say let's suppose we want to update this particular so so we the age is right now the age of a digit is 24 we want to update it to 40. so what we need to do is we need input method we need to pass all the data which we whether we want to update that field whether we want to update or not but in patch method that supports partial updating that means we only want to pass that particular field only so let's suppose we want to update a widget so we want to pass the id okay so we want to pass the id first of all and then we want to pass the name abhijit then we want to pass h which is the which will be updated it that will be let's suppose you want to update it to 34 this this will be the response but in case of you can say patch method it should only take two things the first one will be id on which i do want to update and on what's the age let's suppose this is the difference between put and patch so this patch method supports partial updation and push put method doesn't support partial update okay let's check so and also patch method we need to add partial equal to true so it automatically like django this remote understand that this is a partial method we want to run the partial serializer and in this particular field we want to add this thing so let's now add one thing first also we need to like you can say add a data field so let's do this okay put and we need to this thing we need to copy this thing this over here okay so let's suppose we want to update age 256 and we send the data you can say the data is updated but if we like remove this thing then what will happen it will throw name is required okay but if we do this using a patch method it will work fine we don't need to add anything okay okay so let's create this thing so we okay let's do this thing also we need to pass the object also sorry i will just forget that thing so first of all we need to get the object is equal to person dot objects dot get id is equal to uh data of id and we need to pass the object also i was just mistake forget this by mistake okay so also like uh one more thing that in patch material we need to pass the object uh we want to pass the updated data and we do partial equal to true and now if we go do this particular thing it's work absolutely fine let's suppose this thing and let's like able to create one more tab we will get the data so right now the age is 78 so let's suppose we want to update that is to 12 and hit now you are able to see the updated thing so let's suppose we want to update the id of 3 and let's suppose you want to update the name of id equal to three so we can do testing id uh name is equal to what can be a name we can type uh madan and let's hit okay and if you will get the data you will be able to see the updated data as well okay so now let's see about how delete works so let's check l if else you can type and first of all we need to get the data that is gonna be request dot data and object obj is equal to we can copy this thing from here okay and let's do this thing obj dot delete okay and return this phone you can type like message and here we can return the message that is gonna be data deleted you can say uh person deleted that will make more sense person deleted and we should be very conscious when we are using get because when it don't get any kind of data it returns it raises an exception so let's see so first of all let's do a delete method okay and the id is three so let's see uh method delete is not allowed we need to add it over here patch after patch we can add delete let's hit now okay now you can see like the person is deleted and when we get the data you cannot see after id two it directly jumps to four let's suppose we want to delete fourth number data okay uh now you can see we won't be able to see the four numbers data and we can do five five also and for six also for us seven also okay six one is also deleted now you can see one two and seven directly uh i hope like you people now able to understand so these were the this was all about rest you can say api view decorator how we can do get data how we can apply post method how we can do put patch and delete and this is the most ask interview question that was the difference between put and patch request so you can like say like in patch it supports partial updation on input we it don't support partial updation and you can like tell about what partial updation really means so this was total video about you can say api view decodator now we are gonna see the advanced part of django test framework okay so let's see how we can validate data in django serializer because the data which is coming from the front end we cannot rely on that data so the data is correct so let's suppose we want to only save a person whose age is greater than 18 so we we can we need to add a validation that the person the age which is coming from the front end that should be greater than 18 shouldn't be less than 18 and same applies to name as well so we we don't want to save a name which contains any kind of a special characters like elf uh you can say add the rate symbol any symbol which is which is which is considered to be a special character so let's see how we can do that this first of all we will like see how we can add a validation like a person should be so the age of the person should be greater than 18. so we need to add a validate method so def validate and it takes a data parameter so the data which is coming from the front end which that gets goes on here and we can check if data of age is less than 18 so we can if if the age is less than 18 we can raise our exception so we can light like raise serializer dot validation error age should be greater than 18 should be 18 and we can return the data return the data so let's test this api so we can go to this okay post and we can just copy the data we can first of all we need to run the server python okay so the server is running we can get the data so you can say these are the data so we can let's add data okay and we can post so let's add a name of uh rohan okay let's hit and the 8 should be ages 12 so it's returning an error 8 should be greater than 18 and we can as you can see the validation is working right now so let's suppose i pass age of 19 and it successfully saves the data okay and uh let's uh like like this is for the if you want to add more validation you can add over here so there is a different method also you can write def validate underscore age okay and self data okay so this also works we can print the data what we are getting and we can return also so let's suppose we want to check this one so you can check 19 we are getting so here we are getting the single parameter and we can apply the same logic for a single field in over here so let's suppose you you have one more field called name so you you will like you will add a prefix of validate underscore and add the name of the field so let's suppose here it's age so we can add h so let's suppose it it's d o b we can add d o b okay so now let's suppose we want to add uh like you can say or for validation or whether it can the name shouldn't contain any special character so let's do that thing also so let's see so what we can do is we can paste over here so like right now we have added all the special characters in a string which is called a special characters and this contains all the special characters you can use regex also but for this particular video we are using uh string also so we are looping on it that for c in data and aft after that we are checking also any character contains any this thing also so let's check this thing so let's suppose rohan has an especially character of add the lead let's send this so you can see it's showing another that name cannot contain special character and also that let's suppose we add 12 so it will first of all go on this one so let's suppose we remove this okay okay ram and you just 18 so it's not working right now so you you are like now ready to how we can add validation in jungle so you can add more validation let's suppose you have 10 or field or three or field you want to add validation on three for field you can add this way or either the way you can use prefix validate underscore okay so now let's see how we can see like the foreign key data and what's depth in django like okay so let's suppose this the class is person and we add a class of color okay and we create models dot model uh color name is equal to modals dot carefield of max length of 100 okay and we also registered this particular thing to our self dot color name okay let's add this particular model to here so that we can see admin dot side dot register color okay so again let's migrate this python manage dot py make my creations and python manage dot p by migrate okay uh and also we need to add one foreign key so that we can see how we how we treat foreign key in jungle s framework so models dot car field so the foreign key should be foreign key and a foreign key is added to that color model and on delete models dot cascade related let's add the related name so that we can add a reverse mapping to it color okay so let's again make my creations on my great uh there was an errors let's first delete the my creations okay and add another truth line to over here because we have already added some data so it won't work null true blank equal to true okay make my creations and my credit okay now let's run the server first of all let's create a super user so that we can add some dummy data user okay now let's run the server boot up the server okay so now let's go to the main site ah now we got some color let's add some color red now add some more color uh blue now let's add some more color green okay okay red blue green and uh all we need to do is we need to add some colors to the person model as well so we need to add over here nightmare dot sight dot that just a person okay now add some dummy data so let's suppose this one like blue this person likes green and this person likes thread okay so now when you get this particular api so you can see it's returning color null color one so you can see like it's returning color three so but we are not able to decide what three denotes so it can be like red color it can be blue color so what we want to do like we want to show uh the color which rohan likes so what we can do is we can add one more thing and first of all let's only show that data which has something color okay so what we can do is we can in the gate api we can like do this thing person.object.filter color is null equal to false okay now let's cut this api so you can see now we are only getting the object who which has some colors okay so like we are not able to understand what this one denotes what this three denotes what this two demons so what we can do is first of all we can go in the serializer and we can add one more field that is called depth okay so we can depth to one so let's see what we get so when we add depth equal to one that means like let's suppose you had like some fields which were in this particular foreign key model so what it does is it gets all the data from the from that particular model like you are like we have only two fields for the first one is id the next one is color so that is fetching the django rest framework is fetching all the fields from using the depth keyword okay so but in like some criteria we don't know how to show all the fields we only want to see show this color name and let's suppose we had like some five fields one was id one was color name another one was color hex hex code and another one was color url but we only are interested in showing this particular color name so we cannot do this by depth so we need to write one more serializer which will help django zest framework to serialize the foreign key data so what we can do is first of all we can create a class of color serializer which will help django's framework for understanding that what data we want to show in that particular uh field okay so let's just copy this particular thing from here okay uh now let's add class of meta and let's add model is equal to uh the model is color okay and now we want to add a field so let's suppose fields is equal to uh we are only interested in one field that is called color underscore name okay uh here it's color name so it's working so now what we can do is uh instead of passing depth equal to one we can do this thing we are like connected with color so we can copy this thing and we can color is equal to serializer uh we can type color serializer and we can also type many equal to three okay so once we do this and let's again go to this field okay maybe server has stopped line number three let's do this thing okay let's remove because we have only one data okay fields so now you are able to see what data we are serializing is getting only that data so let's suppose um in future we want to show some more data like let's suppose we want to show id also we can add id over here so it will directly show id over here and you need to also understand like what's the difference between depth and uh you can say serializer and a lot of interviews directly interview will ask what's the difference between depth and when we are serializing with the another serializer class so that difference is see when you write some signalizer it gives more control to you that you can show you it's up to you that you want to show this field or this field or this field and but in depth it realizes all the data which is on the specific or you can say different model okay now i think you are able to understand what's the difference uh how we can say lies uh foreign key data also this is very important question when you when you are like giving some january framework interviews most of the interviewer directly asks that what's like how you can sell the foreign creator so i think now you are able to get so also let's suppose what serializer method field in django jungle says framework so let's suppose uh this person we want to show some country so let's suppose this person which is belonging to this this person we want to show some country so what we can do is we instead of add we can add one more field that is called country and we can like pass the data but let's suppose we want to say add one more three field so we can add country is equal to serializer dot serializer method field okay what you now you can write some custom method for this country all you need to do is you need to add a prefix which is called get so you can need to def get underscore country self and obj and you can return india so let's let's hit this particular api so you will be understanding that what it's mean so you can write some custom method also so let's suppose you want when you are like fetching this api you want to grab some data which is under some other table so you can do that thing also so you can type like this thing so [Music] color underscore obj is equal to color dot objects dot get and we can like that id is equal to obj dot color dot id and we can see lies the data so what we can do is we can like return instead of directly right i like to return link this thing we can do something like this thing so color underscore name which can be we can which we can get from color obj dot color underscore name and after that we can price hex code so let's suppose we are generating hex code with the help of python function so we can type like we can pass the red color that string and it will automatically generate the hex code so we can do something like this hex of zero zero we are just assuming that the hex hex code for all the colors is zero zero zero let's hit this api now you can see the magic happens okay so the country and uh you can like let's suppose we want to name something some something else so we can type color info then you need to change by the same country so all you need to do is when you when you're using some serializer method fill you all you need to add a get underscore prefix and when you are validating the data you and you only had to add validate underscore prefix but you are using when you are using some c like serializer method field function or you can say this particular method you need to add prefix of get underscore so now let's we if we'll hit so it will automatically change the key name so now the key name is color info i hope like you now you people are able to understand the data how django serializes internally works so let's move on to more serializer classes the first class that we had is this one serializer so let's see what this realizer means so when you want to write all the functionality custom okay then you use this particular serializer when you want when you don't want to use any internal functionality of module as a any internal functionality of hyper model serializer when you want to customize write the full custom logic then you use serializer class so let me show an example for you what like basically this class is used for the you can say validation so let me show you an example let's suppose we want to design something and something a lot login page so when you are storing some data from the login you like a user table you cannot use model sigilizer because when you are like if you will you can basically use the model serializer but generally we don't if you want to just serialize the if you want to just validate the data so let's suppose we want to write a login serializer okay and we just pass this class okay and now just all you want to is we want to check whether this particular the data which the the data which we are getting on that in the form of request packet contains this data or not so let's suppose email is equal to serializer dot cal field it should be a cal field or you can say email field and let's suppose you want to add a password okay password is equal to serializer dot uh it should the password should be in a car field okay so now we want to design an api so let's go over here in the this one okay so let's add the rate api view okay let's just copy paste it will help more it will only accept post data so let's remove all the things get okay and now therefore login and it will take requests and after that let's see like the data data is equal to first of all we will do request dot data and after that we will do see realizer is equal to login serializer and we just pass the data data is equal to data and we just check serializer is equal to if serializer dot is valid we just do something we can like get the data that is called data is equal to serializer dot data or you can say clean data or you can say validated data okay and if something is wrong in the data we can like return a response of serializer dot errors and we can make it done we can just copy the response from over here just copy it success and let's add the route for this url so we can go and api urls path um the route will be logged in slash on the function it will call login and let's import this function as well okay so let's hit this api and see what we get okay login let's hit post body is okay now you are able to see that it's to like get related it's email so let's suppose we add a email and we just type apigee and we just type of password is equal to one two okay so it will automatically shows an error that it's not a valid email address and let's type gmail.com let's hit this so you can see message equal to success and we are able to let's suppose use serializer dot let's do this thing okay see laser is valid okay we have we haven't printed the data okay let's do this thing now you are able to get the data email which we have passed from the front end and the password which we have passed from the front end so i hope like you are now able to understand like how this thing works and also we can use validate another thing as well uh let's move on so in this particular video we will see about what is an api view in django res framework until now we have seen like api view decodator and let's see what's the difference between api view decorator and api view so basically api view is in class which helps you to customize your logic like let's suppose in api view decorator what you do is you write different kinds of method that get post put bad delete you don't have any class over like type of a class whereas class helps you you to like add a level of encapsulation which helps you to like combine your code so let's suppose you want you create a class of person and you like add all the method and like the code reduces dramatically like if you like see here you can like if you request automated equal to get if lf requests are not equal to you don't need to do an api view all you need to do is you need to implement some method that def get uh so if there is a get request the get meter will be called if that there is a post method the post method will automatically gets called so first of all let's import api view decorator uh like api view class so it's a class okay so let's first of all import over here from red stream up that views import api view so what we are going to do is first of all let's create a class so the class name we can name as per our like thing so we can we will name it person because it will hold all the apis which a person can have like all the cred apis basically it can have get api person can like the front end should be able to get all the person should be able to post the person should be able to update a person and should be able to delete the person so like we have written a class of person and we just pass api view decorator over here and now what we need to do is we need to implement some methods that if def get so let's suppose if there is a get method it will automatically gets called let me show you with an example so as as you know like in python we need to pass self in methods of a class so we are passing self after that we need to pass request and let's just written an empty response so that you can you people can understand like what's happening behind the scene so message is equal to this is a get request uh let's implement some more method we get which we have in api view so first of all let's implement the next method is called post method so let's update over here that's it's a post and after that's over here also okay so get post and same applies to the next method put patch and delete so here we will write put and here we will update put as well and here we will write patch method okay and now we are gonna add this patch then delete and complete this and delete okay so now like get post put patch delete we have like these setup method and now what we are going to do is we are going to like add a url for this but like when you are dealing with api view decor api view class you need to add your url in a different manner okay so let's suppose uh we have like person okay and like we can say person api class so that it can bifurcate we can differentiate that this is a different method and this is a different look or we can like type persons okay so you can like when we call this out person it will call the api view decorator method and if we are writing persons uh it will call the api view class so we will call person dot as view okay so we need to add this thing so from let's suppose let's type this api okay so that it shouldn't collapse with the model name because we have the same model name here person and if we will name same as this one they make and collapse so we can write api okay person api is is the class which handles all the like you can say five method which are like important for the you can say handling uh person classes okay so now let's test this thing uh okay first of all we need to check this one so let's see other let's run our server python manage dot py run server and let's persons so once we hit this you are able to get like a this is a get request and if we do the post method you can say like it's a post request and when we do put you can see it's a put and when we do patch you can see it's a patch and after that when we do delete you can see it's a delete method so now you are able to understand like what how how the methods actually works okay and you can also see like how your code reduces dramatically like you can see like we have written those this much of code for this thing and we need to like add one more thing that okay so if if it's a get method we are checking if requests are better equal to get it automatically like edges if if it is a get method it automatically automatically calls this method get and if like it changes according to the method so let's implement the logic so to implement the logic we can copy the data from copy which we have written over here so this one is totally similar to this thing okay so you need to remove this one and this one okay and we can remove this thing also and for the post we can just copy this thing it will okay just copy and paste it over here okay now our post method is also done let's check about what let's remove this thing go for patch okay patch and let's go for delete okay so now like you are able to understand how this actually works so let's test this thing so once we are doing a get request over here so it should get we are able to get all the data and if we are posting the data it should return an error okay it should because we have already passed some data over here so let's suppose we add an age of 45 okay and we need to pas if we want to pass the color as well let's suppose we pass color of 2 okay so it will automatically add the color of 2 and if we do a patch method so let's suppose we want to update id is equal to first of all let's add id is equal to 17 okay and we want to add name of ram mishra okay uh let's do this one it should be patch so once okay so once we hit send uh okay this is something error h key h okay we have when like it's adding a validation error so if the if data dot get age and if data dot get if if there is data dot get age and or let's remove this thing for this particular thing okay so let's hit now you are able to see like we are able to update the data and let's suppose we want to delete the data you just need to pass the id the person is deleted so this was it for the api view this was a pretty like a very short kind of thing because like you have already we have already seen about all the all these api how do how do we write all these kind of logic all we need to do is what is an api view so api view is in class which helps you to encapsulate all the methods which are which a cred api needs let's suppose we have five kinds of method the first one is get post the next suppose put patch and delete so it all it integrates all the method like you don't need to write any kind of extra logic it automatically detects what request method is called and it automatically calls the method so let's suppose there is a post method it will automatically call this post method or if there is a put request it automatically will call put request as well so this was it for the like you can say uh ap view decorator api view class sorry and uh like when you are registering the route you need to add class name and dot as view also because if you will don't write this it will it will not accept this as a view so you need to do this thing also so let's move on to the next part so let's see about what our model view sets in jungle this framework so what django says framework says you would all need to like focus on your application you don't need to focus on your like crud api's whole thing because django's s framework has a class that's called model view set which is capable of handling all the credit api which you have which you write so let's suppose we have used till now we have seen about what are avi view decorator so how we can create the data how we can write all the cred apis you can say get post put that patch delete and we have also seen about api view so like uh django x framework is has a class that's called model view set which is capable of handling all the credibility in just two line of code so let's see how it's implemented so first of all we need to import the model view set so let's import that particular thing some rest framework import view sets you can say dot model view set okay so let's do this thing let's import and we just name it as a class of people class people view set okay and we need to pass the view set class view set dot and we need to add a serializer class that and rc laser class is people serializer and our query set is what person dot objects dot all okay now all fourth cred you can say five credit apis are done all we need to just copy this particular thing and just go in urls and just import router so like to register api views we use as view but to register as router or you can say uh this is the model view set we need a different kind of mechanism that's called router so let's import the router so first of all we need to go down and just see where it's implemented just let's just search it router so from this framework import router okay and the default router and all we need to is pass the people view set class let's import people we view set class and after that we just pass over here and just type people let's name as people and let's add our path okay and let's import include as well so now we are good to go and let's head this api we need to go this way and once we hit this so you can see all our cut apis are done so let's suppose we want to add some data we just do this thing people and just hit and you can say it's now created so let's suppose we name something okay let's hit this and when we get the data you are able to see like we are aman gupta where it is now done so like you can see the magic we just wrote two line of code two or three line of code and our all the credit apis are now done so this is a like you can say this is a like effectiveness of django's red stream up like it's so like easy to implement all the credit apis you all you need to just focus on your application layer you don't need to focus on credit apis on the all these small stuff so this is how it is implemented like model view set and you can use something so let's suppose we want to add a search functionality over here so what we can do is so we have like some methods which we i want to show you it's like let's say go over here retrieve mixin so it's called list method okay so let's implement a list method so def list and it takes self and if you want to modify the behavior of get api let's suppose we want to add a search filter so let's suppose i pass abc and we want to get all the data which is like which starts with a b let's suppose i want to filter ram so i will just type ram over here as in as over here as equal to search equal to r okay so it will get all the name which are in the database as r okay so let's implement that functionality so first of all let's get the search parameter let's first let me first show you how this works okay response status of 200 let's hit this and now you can see like it it it it's not called in the inbuilt method it's calling it's calling this much at this list method so let's suppose search is equal to request dot get dot get and of such and let's see uh first of all let's implement the query set query set is equal to self dot query set okay so if search so [Music] query set is equal to query set dot filter name underscore underscore starts with is equal to search okay and all we need to just realize this thing so you realize nice is equal to what's that thing people serializer query set man equal to true okay and just we need to pass the data over so let's do this thing so let's suppose we have pass r over here and it will only fetch the data which which starts with r so let's suppose we pass a so it will only fetch the data which starts with r a and if i do for a so we can get all the data which is starting from a and we type am so we can only get the data which is which starts with am and let's suppose we want o data so we don't have any kind of a data which is like any kind of data in our database which is starts with o so it's returning an empty list so now you can see see how we can overwrite this method so let's suppose if you want to overwrite some functionalities of model you said you can also do that thing because like this framework is when like you are dealing with object oriented framework so if you want to modify any functionality you can modify any of the functionality in django less framework that's the beauty of changes framework because it gives you so much of the thing like other framework are not capable like if you like write something in node.js you won't be able to get all these classes and all these things uh but in django's framework you get all these kinds of data and it's very insane level like when you build some application you feel the power of jam with this frame like that yes this framework has something and when you go to some other framework you miss the the okay this functionality was there in january why don't in this particular framework so it's like it's very the beauty of january's framework okay and the same goes to the other thing so let's see about status in jungle this framework how the statuses are manages because right now we are passing like if you will see in postman it's we are getting a status of okay let's suppose we want to raise some exception if some exception raised then we want to pass 403 we want to pass 500 status quo that how we can do that thing so we need to go on uh let's search status codes okay so from this framework let's import status okay and uh like you need to see what are the successful status what are the direction status what are the client errors status what are the servers status so you can pick any of the status you need to understand like why we send 200 why we send 202 why we send 206 so you need to read all the things but how we can return the status we will see in this particular video so okay so let's suppose we have imported the status so we can just return this response dot status code so status is equal to this thing so let's suppose we want to pass status of 200 uh 201 so we can do status of http 4 4 not found instead of we can do we can say no content okay so let's go and check this thing so you can see it's returning 204 and if we want to like add some status of 203 or 201 let's suppose we want to pass 200 so status is very important because most of the front-end developer use all these status to check whether like uh to show the error messages or to show the status messages you can say so when you are writing some apis you can use this status code so that it will help more thing like if like the front end developer will understand a lot of things that okay this is a successful status okay this is a like error status so we can show some other messages and uh if the like if someone has passed some wrong credential so you can pass you can say client error bad request unauthorized okay so these kind of status all these statuses are available and you can like read more about it that then why like why will 10 406 why we return 412 and all these kind of statuses okay so let's move to the next part that is called permissions in jungle permissions are very important let's suppose you want to show all these data to a person like before jumping directly into permission we just need to see about authentication that how we do authentication in chango's framework okay so django this framework directly comes into you can say authentication that is called token authentication so we will start with how we can authenticate with token authentication so let's go it's authentication okay so all we need to do is first of all we need to add all these things let's framework so that it can start using the authentication class okay so let's paste this over over here and after that we need to add one more thing that is called res framework dot or token so we can add that thing also in settings.py file installed app that's framework.org token and after that we need to make my migration make migration so let's do that thing so python manage dot p by migrate so it will register one table that is called token so let's run the server python manage dot py run server so let's create an api which will register the user and then it will log in the user so let's do that thing so we can do this thing and using you can say let's do this first of all let's import the user model from django dot contrib dot dot models import user and let's create an api for registering so we will use api view decorator api view class class register api and it will take api view and we will call the pose method so it will take self request and we need to check like uh to create any the user we first of all we need email we need password we need username these are the three things which we need to register any user and so for that particular thing we can create you can say as register serializer class serializer serializer dot serializer first of all we need user name okay so serializer dot car field and after that we can use like we need email so is equal to serializer dot email field and after that we need some password so we can copy this thing from here and these are the three things which we require to create an user so username email password and let's go this thing so let's import the serializer also let's just see laser okay so let's check data is equal to request dot data and serializer is equal to and we need to check like whether this user ha is already in the database or not so we can add the method called validate okay so let's check first of all we can pass self data and check if data dot username so we can check like if user dot objects dot filter username is equal to username okay so if if there is a user user with the same username dot username we can return an error dot exist we can raise an exception so there is uh serializer.validationerror dot validation error username is taken and we can do similar kind of the thing to for email as well so we cannot have a user that has a same you can say email so let's import that thing also from django dot contrib dot auth dot models import user and let's do for the you can say email as well okay so we have written a filter for username and email and let's check if serializer dot is valid so what we can do is we can create a we can write a create method or if there is some error we can like first of all let's check if nothing less dot e valid we can write an error message written response okay status is equal to false and message is equal to serializer dot else and we can also pass the status code so status code can by pass status dot so we can get the status quo from here so let's suppose search st a t u s status codes so we can pass what status code so we can pass bad request okay and if the like data is successful we can call this thing serializer dot save and we can return response status of true user created status is equal to 200 that status 201 okay okay so now we are good to go and also we need to implement one method in the serializer class that is called create so first of all let's test this api so what we are okay so we need to go in the api urls and let's import resistor api view and path will be registered okay so now let's test uh the dot as view level we have to add as we because we are using api view okay so now we are good to go and our like server is rebooting and let's hit let's post to so okay so it's throwing an error and you can say 400 bad request so let's add a username that is abhijit and email that is called the rate gmail.com or you can say xyz.com okay and let's add a password of one two three four okay so now not implemented error because we haven't implemented that method create we have as we i have already told you so we need to add that thing as well so first of all let's go to dev create okay and it will take self validated data and once we print the validated data you can see the data which we are getting from the request response okay so you can say user name apigee email and these are the data which we are getting so let's create this particular thing so user is equal to user dot objects dot create username is equal to validated data of user name same applies to for like you can say email in email is equal to validated data of email and to save to or you can say set password we just need to call a set password method dot set underscore password that's gonna be validated data of password and just return that validated data okay now let's test this thing so user created and let's check our database uh uh user so now you can see a widget at the red xyz and if you want if you want to save like first name last name you can add over here that we also need some fields that is called first name last name so that it will help you out to integrate that thing as well so now we are good to go and let's suppose we i want i'm saving this particular data again so it's showing user name is taken let's suppose i change the username something else okay ab1 so now it stored another email okay maybe this is something wrong email address okay here we need to pass email okay so now username is taken email is taken okay now it's good to go so now let's go for you can say login api and then we will and also like we need to do one more thing that is called token.object.create because we are not creating a token for the user as you can see over here so if you are like creating some token you can you need to do this thing token dot object dot get okay so we can do that thing in you can say login api as well so let's do that thing in the login api or otherwise if you want to if you want to do this thing you can do this thing over here also like once the user is created you can create a token so and you can also see we have a token table as well okay so if you want to create a token you can just select the user and it will save the user and the token is created so that we will do in the login part so let's go to the login part of the like view so let's create a class called login api so class login api it will also take api view and we have already written the serializer for this class that's called login serializer and we will use we are going to use this particular serialize in the in this particular class def post and we will take self and we will take request okay so data is equal to request dot data and uh what now we need to do is first of all first of all we need to check whether life with whether we are able to get the data or not serializer is equal to see live uh login serializer and uh like we need to we can copy this thing from here as well okay so let's suppose the serializer is failed or you can say is not valid we can return this thing and if the serializer is validated we can check that thing okay so we can check over here so we need to improve import some method from django.db.org we need to import authen ticket okay okay so this is a like pi uh django stuff so we have authenticate method that helps you to like you can say that helps you to you can say this method checks whether the username and password are correct or not so if the username password are correct it will return the user object so we can return so token so we need to get the token token is equal to uh we need to get import the token model okay so token dot object dot get to create user so we can return the response written response so we can return the status of 200 and okay and we can pass the token str of token okay now let's check uh test this api as well so [Music] okay so now let's test okay so now let's do hit the api okay key error api login whatsoever username see laser.data okay a print serializer.data and let's see what we get over here okay we have pass email so we need to do username and password username it should be cashfield okay let's hit integrity error not null constant field uh user dot id what's the token.object.get so let's print the token as well print token let's hit let's see okay there was a not null constant feel how's this possible okay like maybe the username and email is does not exist okay so we need to check if not user so we need to return the as a response as well because uh like let's suppose the password and username is wrong we haven't so the error that your password and are wrong so you can type invalid credentials all the username is not found invalid credentials so let's test [Music] i think this is correct okay what's the error print user none houses possible a widget let's register one more person so that maybe there can be some issue register okay page not found api slash register what's happening okay user created so we can copy this data which is we can paste this data and it's again throwing another invalid uh let's see what's uh so here we are able to like get the data okay the password is not said because the like the problem is we haven't to call the save method okay user dot save buying padlet user dot save okay so now let's again do this thing so let's do a bit sheet of two and two and just save it so let's check whether this time we are able to successfully save the password or not so now the password is done so we can just copy this and we can paste this and we can so now you can see like we are able to get the token okay so but now how we can use this token so let's suppose we have an api and we only want to show all the data to the like authenticated user so let's suppose we have created an api this one uh you can say here api view okay so we only want to show the data to the person who is authenticated and not to the person who is not authenticated so how we can do this first of all we need to do add the token we are able to get the token so we can use the permission classes in django so let's see how these things are implemented so the default so like we have already done this thing maybe uh we need to go in the settings dot py file just go in the down and we are using the default authentication that is called basic authentication and authentication so we need to add a default permission class that is called is authenticated okay or you can say allow any we can use allow any and we can add that thing okay so now let's import this is authenticated and let's go to the views and first of all let's import and we also need to import the permission classes that what permission classes is this you can say uses is using so first of all we have integrated and uh and we can see one more thing that's called okay let's let's try with this thing so permission classes is authenticated so let's go to that this you can say class okay and permission authentication let's test this thing and the route name the route name is what uh pp persons and let's hit so now you can see the permission credential are not provided so we can go in the editor and we can just we need to add this thing that's called you can say js1 authorization and the token which we got from the this thing okay so we can add token let's hit authentication are not provided because we haven't added one more class so let's add this thing also so from this framework import to this thing okay and we are using token authentication so we'll we will import token authentication okay and we need to add authentication class that is called token authentication django this framework will get information that what authentication cloud class it should need token authentication uh just remove this thing so like now let's test this api so now you can see we are able to get the data and let's suppose we turn off this thing and when we hit this thing you can see authentication credential was not provided so if you want to add some authentication credential to the api view you can use this method like permission classes is authenticated and you can see like authentication like sorry permission class you need to add is authenticated or you can pass allow any so that anyone can access whether whether the person is logged in logged out or not or one more very interesting thing so if you want to get which user is authenticated you so you can type like what print you just do this thing request dot user so let's they do this thing so you know first of all we need to do this thing so you can see a budget 2 is logged in so let's suppose i change that token or you can say i change you can say let's suppose we we the token is for some other user so we will get that user as well so let's create and test this thing also admin token and this token is for apigee ab1 jit let's paste this thing hit this uh now you do this so now the user has changed so this is the way how we can authenticate how you can register users how you can log in the user how you can add permission classes which will allow only authenticated user to see the data or to re to make a request on that particular route so i think now now you people are able to understand so now let's see about what's pagination in django like pagination is very important let's suppose you have like thousands of or you can say lacks of record so you cannot expose all the data in one api because like your api will be so you can say heavy so you it it won't be able to able not able to manage that thing so you can use pagination so let's go to the pagination classes and to implement that pagination class you can use this thing so let's suppose we could just copy this thing and in the settings that people we just paste okay so for pagination we can we have a lot of classes that is that is called limit offset pagination or you can say cursor pagination and other classes so let's add this thing okay okay uh i personally used to write a paginator a custom paginator so let's add that thing helpers dot p y and let's add a paginator def paginator so let's see an example of pagination django s framework so first of all we need to import from django dot co dot paginator and let's import the paginate class okay and after that let's get the page so what's what's the current page so request dot get dot get page and comma often by default it will take one as an argument and let's add a page size so like right now we don't have a lot of records so we will like page any the pagination site will size will be two let's suppose three let's suppose three will be the pagination side so you will only be see only be able to see three records in one page okay and you can increase it to as many as one like basically we have 50 or 40 records per page but right now we don't have a lot of data so we are only doing that for three so again so let's do this and okay so page size is equal to uh it should be page size and page number you can say pass over here okay so now let's test this particular thing and i hope the code is correct so let's add so right now we are able to see only uh other server is okay where is that okay so let's do this hit this page so right now you are able to see only three record one two and three and if we do for page equal to two so right now page two is not found so we also need to check whether like it's a valid page or not so uh like instead of this we can do call so it will written a lot of records okay and let's again test this page okay sorry okay so now you are able to see the next record page two and if you go for page three so 11 12 13 and if you go for page four so it's 14 15 16 and if you go for page five and let's suppose someone like right now the record is like finished off and if we go for six it will throw an other okay empty page number so what we can do is if this is basically returning an exception so we can surround it by try catch okay so try accept exception as e and we can return our response of error okay so that's the invalid page number status of false and we can throw a message that in valid page number invalid page okay so let's again hit this thing empty page where it's throwing an exception online number paginator okay okay i think the code is correct empty page the page contains no result let's surround it over here okay okay now let's uh run this on invalid page now it's working so let's suppose if we do four three so right now we are able to get the data and if you don't pass the page number it will automatically fetch the first record like it will automatically like detect that this is the first page number okay so now we are at a good position and django zest framework so let's go to where very important topic that was action in model view set so like basically when we create an api we have a lot of requests let's suppose uh there was a method uh we use model view set so let's suppose we want to add more route uh like class so we cannot do that we only need to use api view or api decodator of model view set so all these like apis are capable of handling credit apis but let's suppose we want to encapsulate all the you can say apis in one class so we can do that particular thing with the help of actions in django this framework so let's suppose this api people view set was capable of handling a lot of things that cred api so let's suppose now we want to send an email to a person so like again we need to write an api that class send email api view or you can say send api model view set but we want all the functionality related to people should be in a one class that's that's how object-oriented programming works that we encapsulate all the methods and all the functionality in just one class that okay this class is capable of handling this thing only okay so we just use that class and our works our works goes on so but now we cannot do that particular thing so we will introduce actions in django this framework so actions are very important it's like like you can only use actions in model view set uh you cannot use actions in like you can say api view decorator or api view class or any mixins but you can use that particular thing in the view set so let's see what are actions in django this framework so actions first of all we need to go in the model view set view set and then we can go for actions let's search okay view set actions okay so view set actions are like you you only have these kinds of method but in actions uh you can have a lot of other methods as well so by action let's suppose it's a like uh we just it's a kind of a decorator so first of all we need to import that particular thing so we can go to the top of the page and just import it okay and we can import unnecessary things remove it and now again we can go to the bottom of the page and we can add an action so let's suppose we want to add one more api route so it will send an email uh we are not basically sending the mail but just we are going to in a demo purpose okay so let's suppose we want to like send an email send mail to person self request self request and let's do it for detail is equal to false first of all we will see detailed false example then we will see about okay so return response okay so this particular api was capable of hand like sending the you can say uh mail so we just added this thing so right now the our url will look something like this so right now we have api slash persons maybe what was the api route i paid out was people okay so okay so now what we want to do is we want to call this particular action that was uh send email to person so we can add this thing okay uh then timer what's it uh okay so you can see we are able to successfully call this function with the help of actions so now i i hope like you are able to understand you can write the logic of handing the email or you can like you want to do some other functionalities in our api so you can that do that functionality in using actions so let's suppose you want to pass some uh you can say primary key or you want to pass some slug so we can do all that thing also so let's suppose slug or you can say pk and here you like if you are adding some thing over here so you need to make details is equal to true okay now you will be able to the now you will be able to get the pk as well as you can say any slug so now this is your route will not work so you need to add one thing over here okay you can get all this thing so let's suppose we want to get all the uh you can say color where we want to get all the person uh id like we want to get one person using that particular id so our url will look something like this one and send email to person so that we can call this object is equal to person dot objects dot get pk is equal to pk and stabilizer is equal to obj and we can return this thing data okay so now we do this so name pandas okay i don't know why it automatically imports some unnecessary stuff just hit now you are able to see like when you want to pass something and also you can toggle the method as well if you want to if we want this api to be a get api so you can add the method is equal to get and now it will be a get api so we can toggle over here so now it's a get away and if you will hit post post method is not allowed now i hope like you are able to understand this thing as well okay so this was a video for pagination and uh actions in django rest frameworks okay so one very important thing that i want to tell you about model view set is that uh like in some of the cases you don't want to expose all the api okay so let's suppose we want to create an api that should be able to only get request that shouldn't able to like let's suppose we want only two method to apply that particular model view set uh we want get method and we want post method we don't want any other kind of method like put patch patch delete okay so we only want two types of method so how we can do that so model view as you know like model you said is capable of handling all the methods like whether it's a get method put method patch method put delete and everything you can do with the help of model view set but in some cases you you only need to like limit the kind of a method on the apis so let's suppose we we don't want to like once the people is created or once the employees the person is created we don't want to update on that person so how we can do that so we have a like keyword called http method name so here you can the method name which you want to allow let's suppose we want to allow only get and post method so you can add this particular thing so once we like do any other method let's suppose we want to post the method so we will be able to post some method but uh like right now we don't have any correct data and when we do the put method it's saying put method is not allowed and when we do a little patch it will also show another that patch method is not allowed only two methods are allowed the first one is a get method and the second one is a post method and none of the other methods are uh like will work so this is a beauty of model view set and you can say or you can say january's s framework and uh this was a like very important topic like in in most of the cases you don't want to add like you only want to limit the api request you want to like uh you want to apply only get method you only only want to apply the post message you won't be able to like like people should not be or you can say api should not be able to get the data you only want to limit that to post and best you can do this by http method names and you can pass the list of method which this model view set can use so we have covered pretty much each and everything about django this framework till now and this was the entire course that how you can create some beautiful apis using django zest framework okay so now we are gonna use a create a to-do application which will help you to understand that how we can create a to do how we can update or to do using different kinds of methods first of all we are going to use view uh you can say uh api view first of all we are gonna use api view decorator then we are gonna use api view uh class then after that we are going to use you can say model view set okay so we are now at the end of the video and i hope like you people have enjoyed the video and have learned a lot of things about django zest framework that how we can use authentication how we can use pagination and uh how we can create model view sets how we can use module user how you can like whatever status codes and django and what are routers in django and we have covered a lot of topics in django zest framework and uh like it was a quite interesting video and uh i hope you people have learned a lot of things so now what you can do is you can create some kind of apis with the help of using django this framework that will help you to allow like get most of out of this particular video this is it for the video i hope like you people have enjoyed and if you create any projects do comment in the comment section and if you have any doubt do also comment in the comment section i will try to help you out so meet on the next video and good bye and do subscribe to scala thank you
Info
Channel: SCALER
Views: 56,790
Rating: undefined out of 5
Keywords:
Id: s7aINQPGNDM
Channel Id: undefined
Length: 131min 9sec (7869 seconds)
Published: Mon Jul 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.