Okay.
In this video, we will create our own method for rating a movie.
As you remember from this section, from the previous video, we had a method, different methods to
retrieve the movies and retrieve the ratings for that movie.
But I said we will create our own method for rating a movie, and we will do that in this video.
So what we need to do is if we would like to implement our own method, then we need to go to the views.
And as I said before, we have built in the methods already provided by model view set, and we have
that basic five methods.
So we have get all and then we have, if I will open this model view set, I hold control and click
on this one.
You can see you have few methods here and they are correspond to methods.
Create will be this one so it will be the post retrieve.
Will be get on the specific record.
So we will have a specific specific ID in the URL.
Then we have update, which is this one.
So it will be put.
And then this is also patch, but destroy is this one.
So it's delete and then list it's get all of it.
So retrieve and list it's get.
But one is the whole list and retrieve is just one record.
So basically all of the five methods here are available for us in our view set.
So whatever we do movies and then it will be available.
But on top of this methods here, we can also override any of those if we like to use our own, or we
can just specify our own completely new method.
So how can we actually do that?
It's very easy.
So we need to define it as a normal function in the class.
So definition, and then we can name it.
So let's say.
Right.
Underscore movie that will be our name of our function.
And then we do self and we also do request.
So we would like to pass a request inside that function and what we can do here, it's really up to
us.
We have no restrictions here.
Basically, we can have our our logic.
We can ask for the data in our database or whatever we need to do, and then we can return something.
So let's first try to create something very simple and return it, and then we'll see if that that is
working.
So let's create our response.
That's our variable so we can name it whatever we like to name it, and then let's create an object
inside we can call it.
Message.
It's working like that.
And then we can return and then we'll return.
Response.
At the moment, we have no response here.
We need to import it.
So I will import.
From rest framework response, import response, and this is our object that we will return using that
function.
So we do.
First, we response the data and in our case we can response this object that we've created and then
we can decide on the status that we will return and then we can.
Status is if we go here, you can see status 200 here.
That means the status is okay.
So we got it all right.
But we have other options.
So we have 500, 400 and there are hundreds of them.
We can actually invent our own as well.
But there is some standards that we can use.
So let's import status from here, from the rest framework, and we can use it here.
Status Dot.
And you can see here a lot of them are available.
So in our case, we want to respond http 200.
Okay.
That's the classic one that if something happened and it was successful, use a 200.
But you can see here you can pick 200, one, 202 and so on and so on.
So basically that is part of our response and we can get the information like, for example, in our
front end, we could check if that was 200 status, then do something.
Otherwise do something else.
But you have a few of these 200, then you have 300 and you can see here you have 400, which is bad
request, unauthorised payment request forbidden, not found, and so on and so on.
So we have for almost each case a different status number and another popular, it's a 500, so it's
an internal server error or so on.
So it's up to you which one you will pick.
I will use the classic one.
So it's http 200.
Okay.
So http actually I can click on this and I have it.
So basically the response will be this object.
So we'll have a message, it's working and also we'll provide a status 200.
Okay, if I will save it now, then we could use that method.
So if I will copy rate movie name and I will go to our postman.
What we are expecting to do is use on this our URL.
So movies, that's our base URL as we registered that in the URLs.
And then at the end we can use the name of our definition of our method we've created.
So let's try to do it now.
I will send it now and you can see here nothing happened.
So this is not available yet and we need to do something else to do it.
So let's come back here and actually we have some errors there.
So I will try to run the server.
So this is running now.
So I will come back here and then test it that again.
And you can see here, this is not found.
So we have 404 status here, despite the fact that we decided to to use the 200 status there.
So what do we need to do is we need to use a decorator.
So before the our definition, we need to tell what kind of method that will be.
And for for that we can use action like that.
We also need to import action.
So from rest framework decorators import action.
And inside we can decide detail.
True.
And I will show you in a second what does it mean?
Method.
And then that's array of methods.
And we will use method post.
We can decide for more methods here, but for now I will keep it like this.
So basically what we are doing, we are decorating our method with some extra values.
So basically first thing what we are saying is we want to have this detail true.
That means it's not going to be available on the movies.
Slash it.
You will need to provide a specific movie.
So detail true means one specific movie, a detail false means on the list of the movies.
And then we have methods that will accept it will accept method post only so I can provide more methods
here if I will save it now.
And we need to run it.
And then coming back to our postman.
And let's try to do a postman post method now and this rate movie is available on the details.
That means we need to provide also a specific movie here.
So we have our movies, then slash ID of the movie and then rate movie if I will send this now.
And then we have this keyword argument.
PK.
So what does it mean that this unexpected keyword argument.
PK.
PK stands for Primary Key and basically this primary key for this movies, it's by default ID you can
decide which of your columns or of your fields is primary key by by default its ID.
So basically we are passing in our URL primary key here, which is ID, but we don't do anything with
this, so we need to come back here to do the PyCharm and inside our definition rate movie, we need
to also pass PK.
We can also pass it.
What's the.
Default value.
So we will say none is the default value now once we save it.
We can come back to the postman and let's test it again.
And you can see our return is the object message.
It's working.
And this is what we put in our response.
And you can see here, status is 200 as we expected.
So now we have our custom definition for our movies, and then we can write a movie and we can do our
something here and we can have our own logic.
At the moment, this function doesn't do much.
It's just outputting our message as we decided to do.
But here we can do our logic, we can check the request.
We can also output different things based on the request, and we'll do that in the next videos.