WEBVTT

00:02.280 --> 00:05.030
Okay, let's finish our function here.

00:05.040 --> 00:11.180
So basically for creating new rating, we'll need to have three things.

00:11.190 --> 00:17.090
So ratings needs to have a movie user and stars.

00:17.100 --> 00:22.770
So coming back to our views, we have our movie, we have our stars.

00:22.770 --> 00:25.530
So we can actually put it in a variable here.

00:26.130 --> 00:30.270
So stars will be equal to request.

00:31.840 --> 00:41.200
Data and then we can do stars like that because the stars are are in the request data.

00:41.200 --> 00:45.070
So we know so we can assign this to to the variable.

00:45.550 --> 00:48.580
And then what's missing here is the user.

00:48.580 --> 00:55.120
So I can do user and normally the user is part of our request.

00:55.120 --> 00:59.110
So request I can show you the user.

00:59.110 --> 01:01.540
So if I will do print.

01:03.250 --> 01:04.570
User here.

01:06.810 --> 01:10.020
I will save it now and then refresh it.

01:12.040 --> 01:13.570
So let's try to print.

01:13.570 --> 01:15.850
I we'll use the same method again.

01:15.850 --> 01:20.880
And coming back here, you can see user is anonymous.

01:20.890 --> 01:22.750
Why this is happening?

01:22.750 --> 01:29.790
Because at the moment Django can't really tell what user am I?

01:29.980 --> 01:32.340
Because I'm not really logged in.

01:32.350 --> 01:36.700
We haven't done that part yet and we don't have authentication yet.

01:36.730 --> 01:43.570
Normally, if you will pass a token, as we discussed that in the previous section, if you have the

01:43.570 --> 01:51.610
token authentication and with that request that that method, you will send an authorization token,

01:51.610 --> 01:56.560
then request user will have a information about that user.

01:56.560 --> 02:03.400
But at this point we don't have it yet, so we might save that for later.

02:04.120 --> 02:08.920
But for the moment I will just put a comment here and we'll come back to this.

02:08.950 --> 02:12.860
What we can do is we can create our static user.

02:13.650 --> 02:25.020
We know that we have only one user in our database, so we'll do user objects and then I can get ID

02:25.440 --> 02:26.770
equal to one.

02:26.790 --> 02:28.040
That's our user.

02:28.050 --> 02:30.300
We also need to import users.

02:31.860 --> 02:33.650
And we've done it already.

02:35.790 --> 02:37.530
Here in the models.

02:38.230 --> 02:39.300
So we'll copy that.

02:39.300 --> 02:40.410
And I can.

02:41.050 --> 02:42.190
Put it here.

02:43.690 --> 02:51.100
So we have our user, if I will do user now and let's say we'd like to have username.

02:58.910 --> 03:00.020
Send it again.

03:00.620 --> 03:02.990
Coming back here, you can see Christian.

03:02.990 --> 03:10.730
So we select the user based on the ID one that's a fixed ID, and then later on we'll replace with something

03:10.730 --> 03:12.070
more precise.

03:12.080 --> 03:16.310
So basically we could have another way to do it.

03:16.310 --> 03:24.950
So whatever we would like to use this function, we can also request it to have that user ID in the

03:24.950 --> 03:26.120
method itself.

03:26.120 --> 03:28.220
So this one is kind of very robust.

03:28.220 --> 03:30.680
So it will work for the login user.

03:30.680 --> 03:37.190
But if we would like to allow other peoples who are not logging into our system to make us some kind

03:37.190 --> 03:39.170
of rating, you will need to store it.

03:39.170 --> 03:46.040
But we kind of design it this way that in the models we say, okay, you need to pass the user and that

03:46.040 --> 03:48.410
need to be also unique together.

03:48.410 --> 03:50.090
So user and movie.

03:50.090 --> 03:56.870
So in the moment when you decided that you will pass the user, you need to actually create your application.

03:56.870 --> 04:03.210
This way the user will be extracted from the from the person who is doing that call.

04:03.210 --> 04:08.820
So if I'm logging now and I will try to do the rating, my user will be passed to that method.

04:08.820 --> 04:10.920
And this method we have it here.

04:10.920 --> 04:14.100
So it will later on will use this request user.

04:14.130 --> 04:16.680
At the moment we have our static.

04:17.800 --> 04:20.020
So what do we need to do here?

04:20.020 --> 04:23.170
We have movie, we have stars and we have user.

04:23.410 --> 04:28.510
So we have all the information we need to construct our rating.

04:29.080 --> 04:33.490
So what we can do first is we have two options.

04:33.490 --> 04:39.220
First, we will try to check if that rating already exists.

04:39.220 --> 04:46.450
So if we have something in our database with that movie and stars, if we do have it, then we'll update

04:46.450 --> 04:46.780
it.

04:46.780 --> 04:48.910
If we don't have it, we create a new.

04:48.910 --> 04:53.800
So in this way we'll have rate movie one method for two calls.

04:53.800 --> 04:58.390
So it will be like our standard post and put in one method.

04:58.390 --> 05:00.010
So let's try.

05:00.040 --> 05:05.650
We can do try and here we'll try to select something from our database.

05:05.650 --> 05:14.290
And I'm using keyword try here because otherwise if I will do this, might fail and it will break the

05:14.290 --> 05:15.100
whole method.

05:15.100 --> 05:16.450
But we don't want to break it.

05:16.450 --> 05:20.360
We just want to check if we have already that in the database.

05:20.360 --> 05:22.670
Otherwise we'll create create it.

05:22.670 --> 05:33.950
So let's do rating, we'll name rating and then we'll try to select the rating objects and then we'll

05:33.950 --> 05:42.050
try to get and get we need to do user is our user ID.

05:44.460 --> 05:45.560
And then movie.

05:46.960 --> 05:50.140
Will be equal to movie ID.

05:50.440 --> 05:52.660
This user is the whole object.

05:52.660 --> 05:54.070
This movie is the whole object.

05:54.070 --> 06:03.040
So what we are trying to get here is the user is stored as a if we do here this model foreign key,

06:03.070 --> 06:07.720
that's user ID, it's not the whole objects stored in a database.

06:07.720 --> 06:14.560
So whenever we try to get here, something from here, we are saying this user needs to be equal as

06:14.560 --> 06:17.320
user ID because that's what we store in database.

06:17.320 --> 06:20.110
So ID are stored in the database.

06:22.190 --> 06:31.090
Once we have that, we if we have something like a get, then we can do something about it.

06:31.090 --> 06:39.670
So we can do rating and then we can do stars equal to and then we have our variable stars.

06:41.480 --> 06:46.190
And then what we can do is rating and then we can do save.

06:49.100 --> 06:54.020
And then we can create something if that fails.

06:54.020 --> 07:01.040
So if there is no rating with that user and movie, this will fail and then we can catch it here and

07:01.040 --> 07:04.190
then we can create that object from scratch.

07:04.190 --> 07:06.620
So we do accept.

07:07.990 --> 07:08.680
Like that.

07:08.680 --> 07:13.630
And then we can use a similar function as we use here.

07:14.700 --> 07:17.190
So I will copy most of this here.

07:17.190 --> 07:20.610
So basically this time we'll do ratings objects.

07:22.050 --> 07:23.190
Create.

07:23.220 --> 07:30.000
So we will create in this time, instead of ID, we'll pass the whole objects because that will be done

07:30.000 --> 07:30.480
by us.

07:30.480 --> 07:35.910
And then we also need to do stars so stars will equal to stars.

07:36.060 --> 07:41.730
So we are creating here a new object with this user movie and star.

07:41.730 --> 07:43.680
So let's save it now.

07:47.760 --> 07:49.640
And then we have.

07:49.650 --> 07:51.210
We need to run it now.

07:52.480 --> 07:54.070
That's all working fine.

07:54.670 --> 08:00.570
And at the moment we still outputting this, but we can actually customize it.

08:00.660 --> 08:02.860
It depends on what we have done.

08:02.980 --> 08:08.050
So let's come back to the postman and send it here.

08:10.230 --> 08:11.770
And this is invalid.

08:11.790 --> 08:16.920
Later, you can see here we are trying to put whatever for the stars.

08:16.920 --> 08:21.360
But as you know, we put that validator validators in place.

08:21.360 --> 08:24.540
So we can't really put whatever here.

08:24.540 --> 08:27.060
And you can see 500 internal server error.

08:27.060 --> 08:29.460
So we need to put actual stars here.

08:29.490 --> 08:32.820
It's a kind of hint that it's already our method is kind of working.

08:32.820 --> 08:40.290
So if I will do two here, which is accepted acceptable value, I can send it now and you can see message

08:40.290 --> 08:40.800
is working.

08:40.800 --> 08:47.790
So what we can do here, I will go to another tab and I will do get method for the ratings and check

08:47.790 --> 08:50.880
if we actually have something in our database.

08:50.880 --> 08:52.530
Now I will send it.

08:52.740 --> 08:54.570
And indeed we have.

08:54.780 --> 09:04.170
So we have ID this movie one and then we have three stars, user and movie, and then we have for another

09:04.170 --> 09:04.710
movie.

09:04.710 --> 09:07.710
So this is movie two and this is movie one.

09:08.520 --> 09:12.070
Here we try to do a movie two.

09:12.070 --> 09:17.740
So if I will, let's say this, let's let's remember this one.

09:17.740 --> 09:22.690
So because this rating will be for movie two and that's my user one.

09:22.690 --> 09:24.940
And then we had stars two.

09:24.970 --> 09:32.650
So if I will come back here and I will do now Stars five and send it again for the same method for the

09:32.650 --> 09:37.060
same movie, let's come back here and see if that will change.

09:38.610 --> 09:40.570
And indeed that's been changed.

09:40.590 --> 09:49.410
So that means we updating the records, if I will go now and use this ID for rating.

09:49.500 --> 09:51.840
So that will be two.

09:53.410 --> 09:57.910
I will have only that rating for this one, but I would like to remove it now.

09:57.910 --> 10:01.720
So I will click, delete and send it and this is gone.

10:01.720 --> 10:03.430
So coming back to the list.

10:05.970 --> 10:08.550
And we need to to get.

10:10.570 --> 10:14.440
And you can see here I have only four movie won ratings.

10:14.440 --> 10:19.720
So coming back here with the same I won't change anything here and send it again.

10:19.720 --> 10:22.780
I'm expecting I will create that rating now.

10:23.850 --> 10:25.020
It's working here.

10:25.020 --> 10:27.120
And I will refresh it.

10:27.930 --> 10:29.970
And you can see here it's working.

10:29.970 --> 10:37.050
So our function if if the this rating doesn't exist for that movie, then it will be created.

10:37.050 --> 10:39.510
If it exists already, it will update it.

10:39.540 --> 10:44.190
So this is what we achieved here in our method.

10:44.190 --> 10:45.630
So what else we can do?

10:45.630 --> 10:51.360
Because at the moment we whatever we do here, we have our message like that.

10:51.360 --> 10:57.540
But what we can do is we can output back the data that we passed in.

10:57.570 --> 11:04.950
So I will make more space here and we can do a different things for try and we can do different things

11:04.950 --> 11:06.420
for creating here.

11:07.050 --> 11:09.240
So let's grab this.

11:10.040 --> 11:16.230
I will put it here first, and then we'll do something so I can do here.

11:18.030 --> 11:18.420
Either.

11:18.420 --> 11:24.210
That's just a variable and then we can use a serializer to serialize the data.

11:24.240 --> 11:28.460
I will do writing serializer here and I will put it there.

11:28.470 --> 11:35.550
So writing Serializer the same way we've done it before, we can pass to this rating serializer what

11:35.550 --> 11:36.900
we would like to serialize.

11:36.930 --> 11:40.920
We have our rating object there, so I can serialize that.

11:40.920 --> 11:43.650
I can also say that's many false.

11:43.680 --> 11:50.430
So it's only one, uh, one rating and we have that data there.

11:50.430 --> 12:00.210
So the message is we can change it for rating, update it, and then we can in this the same object

12:00.210 --> 12:04.230
we can do, for example, result.

12:05.780 --> 12:09.500
And then we can do serializer data.

12:09.860 --> 12:16.700
So what we are doing here is we select the serializer with our updated rating when we save it.

12:16.730 --> 12:26.190
Then we add more information into our response and we pass that response with the Http 200.

12:26.210 --> 12:31.400
So I will just copy this and I will go here.

12:31.760 --> 12:39.490
So this time we need to also apply this create into our variable.

12:39.500 --> 12:44.210
So we will have our rating here that will be effect of creating our rating.

12:44.210 --> 12:53.840
And then we serialize that data and then we can do rating created this time and we will also put it

12:53.840 --> 12:54.350
there.

12:56.120 --> 13:02.660
So coming back to the postman and let's try to come here and then I will do send.

13:03.680 --> 13:08.180
You can see here, message rating updated and that was updated.

13:08.180 --> 13:14.540
So I can do three here and the start three are here.

13:14.540 --> 13:23.630
So if I will remove that this one because I would like to test how this the output will be when I create

13:23.630 --> 13:26.690
something, I need to remove ID3.

13:26.690 --> 13:30.710
So I'll do three and then change the method to.

13:31.640 --> 13:35.660
Delete and then send it here and you can see nothing is there.

13:35.660 --> 13:40.670
But if I will now try to, let's do four.

13:40.760 --> 13:45.830
I'm expecting the message will be created and the starts will be four.

13:47.040 --> 13:50.520
You can see writing created and the stars for.

13:51.060 --> 13:53.130
So we have our method.

13:54.230 --> 13:55.640
Here created.

13:55.640 --> 14:03.800
So we cover a lot of things here, and I've done that in purpose because I would like to show you what

14:03.800 --> 14:08.300
are the different possibilities when we have our own function.

14:08.300 --> 14:12.110
So what we can do here is we can validate the request data.

14:12.110 --> 14:19.640
We can select some objects from our database using this syntax that we talked about it before.

14:19.640 --> 14:27.620
Then we can do different things based on the existence in the database and then we know how to output

14:27.620 --> 14:30.530
something inside our.

14:31.560 --> 14:32.370
Response.

14:32.370 --> 14:36.330
So this is the object that we actually constructed on our own.

14:36.330 --> 14:37.410
And this.

14:37.440 --> 14:38.640
Is optional.

14:38.640 --> 14:39.810
You don't need to do this.

14:39.810 --> 14:41.100
You don't need to do this.

14:41.130 --> 14:47.190
And then it's all depends on how you would like to do things in our front end.

14:47.190 --> 14:54.870
So if you require some kind of message for your user to be displayed like a rating created, you can

14:54.870 --> 15:01.290
pass it as a message and then the result will be updated results or whatever you need to do.

15:01.320 --> 15:07.110
You have full options here because all of that is our own code so we can decide it.

15:07.140 --> 15:09.510
What will we put in the object?

15:09.540 --> 15:11.220
What will be the message?

15:11.220 --> 15:13.860
What will be the status and so on and so on.

15:13.860 --> 15:21.780
So you can see here we've created a simple method of our own, but it will be very powerful because

15:21.780 --> 15:28.530
we can have one method that will work for for either update or for post.
