WEBVTT

00:02.970 --> 00:03.540
Okay.

00:03.540 --> 00:09.660
In this video, we will create our own method for rating a movie.

00:10.320 --> 00:16.710
As you remember from this section, from the previous video, we had a method, different methods to

00:16.710 --> 00:21.660
retrieve the movies and retrieve the ratings for that movie.

00:21.660 --> 00:28.290
But I said we will create our own method for rating a movie, and we will do that in this video.

00:28.290 --> 00:36.300
So what we need to do is if we would like to implement our own method, then we need to go to the views.

00:37.170 --> 00:46.230
And as I said before, we have built in the methods already provided by model view set, and we have

00:46.260 --> 00:47.970
that basic five methods.

00:47.970 --> 00:56.130
So we have get all and then we have, if I will open this model view set, I hold control and click

00:56.130 --> 00:56.820
on this one.

00:56.820 --> 01:03.100
You can see you have few methods here and they are correspond to methods.

01:03.100 --> 01:07.990
Create will be this one so it will be the post retrieve.

01:08.820 --> 01:12.600
Will be get on the specific record.

01:12.600 --> 01:16.910
So we will have a specific specific ID in the URL.

01:16.920 --> 01:19.680
Then we have update, which is this one.

01:19.680 --> 01:21.240
So it will be put.

01:21.360 --> 01:26.820
And then this is also patch, but destroy is this one.

01:26.820 --> 01:30.600
So it's delete and then list it's get all of it.

01:30.600 --> 01:32.400
So retrieve and list it's get.

01:32.400 --> 01:36.300
But one is the whole list and retrieve is just one record.

01:36.300 --> 01:43.470
So basically all of the five methods here are available for us in our view set.

01:43.470 --> 01:47.490
So whatever we do movies and then it will be available.

01:47.520 --> 01:57.210
But on top of this methods here, we can also override any of those if we like to use our own, or we

01:57.210 --> 02:01.620
can just specify our own completely new method.

02:01.620 --> 02:03.750
So how can we actually do that?

02:03.990 --> 02:05.070
It's very easy.

02:05.070 --> 02:10.150
So we need to define it as a normal function in the class.

02:10.150 --> 02:13.000
So definition, and then we can name it.

02:13.000 --> 02:14.290
So let's say.

02:15.510 --> 02:16.030
Right.

02:16.410 --> 02:21.080
Underscore movie that will be our name of our function.

02:21.090 --> 02:25.650
And then we do self and we also do request.

02:25.920 --> 02:33.840
So we would like to pass a request inside that function and what we can do here, it's really up to

02:33.840 --> 02:34.380
us.

02:34.410 --> 02:36.420
We have no restrictions here.

02:36.420 --> 02:39.360
Basically, we can have our our logic.

02:39.360 --> 02:46.140
We can ask for the data in our database or whatever we need to do, and then we can return something.

02:46.140 --> 02:55.110
So let's first try to create something very simple and return it, and then we'll see if that that is

02:55.110 --> 02:55.800
working.

02:55.800 --> 02:58.350
So let's create our response.

02:58.830 --> 03:05.730
That's our variable so we can name it whatever we like to name it, and then let's create an object

03:05.730 --> 03:07.530
inside we can call it.

03:08.670 --> 03:09.510
Message.

03:13.490 --> 03:16.160
It's working like that.

03:16.370 --> 03:21.110
And then we can return and then we'll return.

03:21.120 --> 03:22.220
Response.

03:24.540 --> 03:26.880
At the moment, we have no response here.

03:26.880 --> 03:28.290
We need to import it.

03:29.120 --> 03:31.580
So I will import.

03:33.460 --> 03:44.050
From rest framework response, import response, and this is our object that we will return using that

03:44.050 --> 03:44.740
function.

03:44.740 --> 03:46.320
So we do.

03:46.330 --> 03:53.260
First, we response the data and in our case we can response this object that we've created and then

03:53.260 --> 04:00.430
we can decide on the status that we will return and then we can.

04:00.430 --> 04:05.260
Status is if we go here, you can see status 200 here.

04:05.260 --> 04:07.180
That means the status is okay.

04:07.180 --> 04:09.310
So we got it all right.

04:09.310 --> 04:10.990
But we have other options.

04:10.990 --> 04:15.460
So we have 500, 400 and there are hundreds of them.

04:15.460 --> 04:17.920
We can actually invent our own as well.

04:17.920 --> 04:21.010
But there is some standards that we can use.

04:21.010 --> 04:28.510
So let's import status from here, from the rest framework, and we can use it here.

04:28.510 --> 04:29.650
Status Dot.

04:29.650 --> 04:33.250
And you can see here a lot of them are available.

04:33.250 --> 04:38.470
So in our case, we want to respond http 200.

04:38.470 --> 04:38.860
Okay.

04:38.890 --> 04:45.070
That's the classic one that if something happened and it was successful, use a 200.

04:45.070 --> 04:49.870
But you can see here you can pick 200, one, 202 and so on and so on.

04:49.870 --> 04:58.420
So basically that is part of our response and we can get the information like, for example, in our

04:58.420 --> 05:03.370
front end, we could check if that was 200 status, then do something.

05:03.370 --> 05:04.960
Otherwise do something else.

05:04.960 --> 05:12.990
But you have a few of these 200, then you have 300 and you can see here you have 400, which is bad

05:13.000 --> 05:17.860
request, unauthorised payment request forbidden, not found, and so on and so on.

05:17.860 --> 05:27.130
So we have for almost each case a different status number and another popular, it's a 500, so it's

05:27.130 --> 05:29.470
an internal server error or so on.

05:29.470 --> 05:32.350
So it's up to you which one you will pick.

05:32.380 --> 05:34.130
I will use the classic one.

05:34.130 --> 05:35.990
So it's http 200.

05:35.990 --> 05:36.650
Okay.

05:36.650 --> 05:40.880
So http actually I can click on this and I have it.

05:40.880 --> 05:45.530
So basically the response will be this object.

05:45.530 --> 05:50.360
So we'll have a message, it's working and also we'll provide a status 200.

05:50.390 --> 05:55.640
Okay, if I will save it now, then we could use that method.

05:55.640 --> 06:00.620
So if I will copy rate movie name and I will go to our postman.

06:00.680 --> 06:06.890
What we are expecting to do is use on this our URL.

06:06.890 --> 06:11.960
So movies, that's our base URL as we registered that in the URLs.

06:11.960 --> 06:17.990
And then at the end we can use the name of our definition of our method we've created.

06:17.990 --> 06:19.790
So let's try to do it now.

06:19.790 --> 06:24.680
I will send it now and you can see here nothing happened.

06:24.710 --> 06:29.180
So this is not available yet and we need to do something else to do it.

06:29.180 --> 06:34.310
So let's come back here and actually we have some errors there.

06:34.310 --> 06:37.340
So I will try to run the server.

06:37.340 --> 06:38.750
So this is running now.

06:38.750 --> 06:42.770
So I will come back here and then test it that again.

06:43.160 --> 06:45.950
And you can see here, this is not found.

06:45.950 --> 06:53.870
So we have 404 status here, despite the fact that we decided to to use the 200 status there.

06:53.870 --> 06:58.070
So what do we need to do is we need to use a decorator.

06:58.820 --> 07:06.530
So before the our definition, we need to tell what kind of method that will be.

07:06.530 --> 07:11.690
And for for that we can use action like that.

07:12.080 --> 07:14.060
We also need to import action.

07:17.200 --> 07:24.280
So from rest framework decorators import action.

07:25.540 --> 07:28.090
And inside we can decide detail.

07:28.960 --> 07:29.770
True.

07:29.770 --> 07:32.410
And I will show you in a second what does it mean?

07:32.830 --> 07:33.580
Method.

07:34.090 --> 07:36.190
And then that's array of methods.

07:36.190 --> 07:38.560
And we will use method post.

07:38.590 --> 07:44.080
We can decide for more methods here, but for now I will keep it like this.

07:44.080 --> 07:49.810
So basically what we are doing, we are decorating our method with some extra values.

07:49.810 --> 07:56.050
So basically first thing what we are saying is we want to have this detail true.

07:56.080 --> 07:59.380
That means it's not going to be available on the movies.

07:59.380 --> 08:00.490
Slash it.

08:00.490 --> 08:02.740
You will need to provide a specific movie.

08:02.740 --> 08:09.220
So detail true means one specific movie, a detail false means on the list of the movies.

08:09.220 --> 08:16.300
And then we have methods that will accept it will accept method post only so I can provide more methods

08:16.300 --> 08:18.880
here if I will save it now.

08:20.520 --> 08:21.990
And we need to run it.

08:23.840 --> 08:26.000
And then coming back to our postman.

08:27.410 --> 08:36.230
And let's try to do a postman post method now and this rate movie is available on the details.

08:36.230 --> 08:40.520
That means we need to provide also a specific movie here.

08:40.520 --> 08:47.900
So we have our movies, then slash ID of the movie and then rate movie if I will send this now.

08:50.990 --> 08:54.170
And then we have this keyword argument.

08:54.180 --> 08:54.650
PK.

08:55.430 --> 08:59.720
So what does it mean that this unexpected keyword argument.

08:59.720 --> 09:00.080
PK.

09:00.380 --> 09:10.490
PK stands for Primary Key and basically this primary key for this movies, it's by default ID you can

09:10.490 --> 09:16.970
decide which of your columns or of your fields is primary key by by default its ID.

09:17.270 --> 09:24.830
So basically we are passing in our URL primary key here, which is ID, but we don't do anything with

09:24.830 --> 09:31.220
this, so we need to come back here to do the PyCharm and inside our definition rate movie, we need

09:31.220 --> 09:32.750
to also pass PK.

09:32.960 --> 09:34.400
We can also pass it.

09:34.430 --> 09:35.180
What's the.

09:36.000 --> 09:37.220
Default value.

09:37.230 --> 09:42.030
So we will say none is the default value now once we save it.

09:43.560 --> 09:47.460
We can come back to the postman and let's test it again.

09:48.820 --> 09:52.270
And you can see our return is the object message.

09:52.270 --> 09:53.200
It's working.

09:53.200 --> 09:55.720
And this is what we put in our response.

09:55.720 --> 10:00.820
And you can see here, status is 200 as we expected.

10:00.820 --> 10:11.350
So now we have our custom definition for our movies, and then we can write a movie and we can do our

10:11.350 --> 10:14.860
something here and we can have our own logic.

10:14.890 --> 10:18.310
At the moment, this function doesn't do much.

10:18.340 --> 10:22.120
It's just outputting our message as we decided to do.

10:22.120 --> 10:28.090
But here we can do our logic, we can check the request.

10:28.120 --> 10:35.740
We can also output different things based on the request, and we'll do that in the next videos.
