WEBVTT

00:01.990 --> 00:11.110
Okay, let's set up our views and serializers to expose our models to our API so I can close the admin

00:11.110 --> 00:13.800
now and we have our models here.

00:13.810 --> 00:18.850
And also I will close this URLs here and also I can do with the settings here.

00:18.850 --> 00:26.950
So we have models we need to create a new file and we'll call it the realizers.

00:27.310 --> 00:27.970
PY.

00:28.720 --> 00:31.510
This is the same way as we've done before.

00:31.510 --> 00:33.550
So first I will import.

00:35.120 --> 00:35.870
Uh, sorry.

00:35.900 --> 00:39.890
From rest framework.

00:40.190 --> 00:43.760
Import serializers.

00:43.790 --> 00:49.460
We'll import serializer and we'll create for each our model a new serializer.

00:49.460 --> 00:53.690
So class movie serializer.

00:57.650 --> 00:59.930
And we'll do Serializers.

01:00.440 --> 01:02.450
Model Serializer.

01:02.450 --> 01:06.350
We've done that before, and then we do class meta.

01:06.800 --> 01:12.770
And inside here we can decide what will be in our serializer.

01:12.770 --> 01:15.260
So first the model.

01:18.740 --> 01:22.370
It's going to be our movie model.

01:22.550 --> 01:24.260
We need to import that.

01:24.260 --> 01:33.320
So from current directory models, import movie and then we can decide on the fields.

01:35.250 --> 01:39.900
So the fields will be actually, that should be equal.

01:39.900 --> 01:40.620
Fine.

01:41.010 --> 01:41.600
Sorry.

01:41.610 --> 01:42.450
My mistake.

01:42.460 --> 01:44.720
So the fields should be.

01:44.730 --> 01:47.850
We have available ID then.

01:47.850 --> 01:48.540
We have.

01:49.810 --> 01:50.830
Title.

01:50.830 --> 01:54.280
And then we have description.

01:54.580 --> 02:03.160
And that's the same fields as are we have title description here and ID is added automatically for us

02:03.160 --> 02:04.120
by Django.

02:04.210 --> 02:07.000
So we have that serializer here.

02:07.000 --> 02:11.140
And then what we can do to speed up, I can just duplicate it.

02:11.590 --> 02:15.190
And then another serializer will be rating.

02:16.830 --> 02:21.360
So we need to also change this model to rating.

02:21.360 --> 02:27.900
And this will also import rating here for rating.

02:27.900 --> 02:32.220
What we have is we have ID stars.

02:34.350 --> 02:38.880
We have user and then we have movie.

02:40.750 --> 02:44.050
And we have our serializers set up.

02:44.080 --> 02:51.520
What we also need to do is we need to go to the views and inside the views we'll create a views for

02:51.520 --> 02:55.210
each of the serializer so as before class.

02:57.290 --> 02:57.890
Movie.

02:58.670 --> 03:03.020
Few said like that and then we'll do.

03:04.200 --> 03:06.480
We need to import that.

03:07.630 --> 03:12.430
So from rest framework import.

03:13.950 --> 03:18.180
View set and this view sets we can use here.

03:19.320 --> 03:21.470
Model view set like that.

03:21.480 --> 03:27.630
So what needs to include their query set?

03:27.750 --> 03:30.330
And that's going to be our movie.

03:31.440 --> 03:37.080
Objects and then we'll do all like that.

03:37.110 --> 03:39.270
We need to import our movie.

03:39.300 --> 03:41.130
So from.

03:42.750 --> 03:51.660
Models import movie also will also import rating from here because we'll use it in a second.

03:51.660 --> 03:56.310
So we have our query set and then we'll do serializer class.

03:56.310 --> 03:59.070
What serializer for this movie view set.

03:59.100 --> 04:03.990
We have Serializers here and that's our movie Serializer.

04:03.990 --> 04:06.120
So I can pass it here.

04:06.120 --> 04:10.560
Actually, the serializer class, it's a tuple, so I can do it like that.

04:10.560 --> 04:12.630
And then also I need to import it.

04:12.630 --> 04:21.330
So from Serializers import Movie Serializer and the Serializer class and the query set is set up for

04:21.330 --> 04:30.570
our movies, then I can duplicate this Ctrl D and then I will do rating view set.

04:31.110 --> 04:36.240
And then what I have to do is I have already imported that.

04:36.240 --> 04:43.570
So in this case we'll select all the ratings and also we'll use the ratings serializer.

04:43.570 --> 04:49.210
And this this one needs to be also imported from serializers like that.

04:49.300 --> 04:55.990
So basically that's a typical setup as we have it here, and we've done that before.

04:55.990 --> 05:04.720
So there is not a new I've done it quite fast, but we know how to do it from the previous section when

05:04.720 --> 05:06.250
we talked about Django.

05:06.370 --> 05:11.260
So what we set up so far, we have our models with a different fields.

05:11.260 --> 05:16.810
Then we create new file serializers and for each model we've create different serializer.

05:16.810 --> 05:22.510
We tell the serializer which model we are using and what fields we would like to use it.

05:22.510 --> 05:29.230
So we in that case, we have to also, in the view set in the views we've created, view set for each

05:29.230 --> 05:29.740
model.

05:29.740 --> 05:38.410
So it's a lot of typing here, but basically we are creating view sets using the built in Django rest

05:38.410 --> 05:45.250
framework view set, the model view set here and we set up the query set which is what?

05:46.010 --> 05:48.830
Set of records from database we would like to use.

05:48.830 --> 05:51.710
And in our case, we would like to use all movies.

05:51.710 --> 05:57.050
And then we tell also what serializer we will be using for the movie set.

05:57.080 --> 06:04.580
We're using Movie Serializer, and for the rating view set, we said we use rating serializer.

06:04.580 --> 06:07.220
So all all of that is already set up.

06:07.220 --> 06:14.390
What we can do also is we need to go to URLs here and register our view sets in our router.

06:14.390 --> 06:17.180
So router.

06:20.280 --> 06:24.570
Register and then we can register.

06:24.840 --> 06:33.100
This view set as we see here, I can copy that and register that on the router.

06:33.120 --> 06:42.590
So the first argument will be movies, which is just a string that will be inside our path.

06:42.600 --> 06:45.870
And the second argument is the view set itself.

06:45.870 --> 06:47.430
We need to import that.

06:47.430 --> 06:48.570
So from.

06:50.490 --> 06:52.950
Views import.

06:53.040 --> 06:54.270
Movie view set.

06:54.270 --> 06:56.040
And we have that here.

06:56.160 --> 06:58.680
Also I will register another.

07:01.880 --> 07:04.160
View that which is rating view Z.

07:04.730 --> 07:14.310
So coming back here, I import view set, use it here and that will be ratings like that.

07:14.330 --> 07:19.610
So I save it now and then I can run it.

07:20.850 --> 07:29.490
And let's see what I can do in our I will copy that URL and let's start to test our application.

07:29.490 --> 07:37.350
So I will do like that and then I can go API and then movies.

07:37.560 --> 07:43.190
That's what I registered in our database and we have some problems now.

07:43.200 --> 07:50.010
Page not found API movie, so let's come back here.

07:50.010 --> 07:51.750
So let's try to debug it.

07:51.750 --> 08:00.750
We'll go to the Chrome and let's go to our URLs like that and then we have admin and we have our API.

08:00.750 --> 08:03.090
So let's go to API.

08:04.650 --> 08:05.490
Like that.

08:05.760 --> 08:09.930
And then we have our movies and then we have our ratings.

08:09.930 --> 08:12.510
So everything looks like it's working here.

08:12.510 --> 08:20.190
So I will click on movies and then topple Object is not callable, so we can see our error there.

08:20.190 --> 08:21.720
And which one is that?

08:21.720 --> 08:25.470
So basically that's a problem when we have.

08:26.760 --> 08:28.800
A tuple that has been registered.

08:28.800 --> 08:30.090
Let me take a look.

08:34.070 --> 08:40.460
And Dave, you said and actually I made a mistake because the serializer class can't accept more than

08:40.460 --> 08:40.790
one.

08:40.790 --> 08:47.150
So in our case, it has to be just one serializer like that.

08:47.150 --> 08:49.100
And then I will save it here.

08:50.300 --> 08:51.830
So that's gone.

08:51.830 --> 08:59.780
And then if we refresh it now, you can see I have empty results of movies just to make sure I will

08:59.780 --> 08:59.990
go.

09:00.020 --> 09:01.940
The same with the ratings.

09:03.610 --> 09:05.950
Uh, ratings.

09:05.950 --> 09:06.850
I misspell it.

09:06.850 --> 09:09.460
So ratings like that.

09:09.460 --> 09:11.440
And I have the same information here.

09:11.440 --> 09:15.610
So what I can do is I can come back to the postman and send it again.

09:18.990 --> 09:20.190
Page not found.

09:20.250 --> 09:21.360
Not available.

09:21.480 --> 09:27.060
And the reason for this is so I will copy that.

09:28.050 --> 09:30.600
Let's try this one.

09:31.870 --> 09:32.650
Ratings.

09:33.640 --> 09:37.750
And you can see empty red results there and movies.

09:40.280 --> 09:41.630
An empty vessel there.

09:41.640 --> 09:43.640
I think I had misspelled it.

09:43.640 --> 09:46.180
So that was movie instead of movies.

09:46.190 --> 09:49.340
So our URLs are plural.

09:49.340 --> 09:51.650
So we have movies and ratings.

09:51.650 --> 09:55.490
So we have our application kind of running.

09:55.490 --> 10:01.010
So we have our movies API working and also the ratings.

10:01.010 --> 10:04.490
So at the moment we have nothing in our database.

10:04.490 --> 10:09.350
So in the next video we'll try to create some records in our database.

10:09.350 --> 10:15.170
But for doing that, we are not going to use Django, we are not going to use this interface and that

10:15.170 --> 10:17.420
is available here as we can use it.

10:17.420 --> 10:24.980
But we'll try to do everything in our API in The Postman and that will kind of mimic what we will also

10:24.980 --> 10:28.910
need to recreate later on in our front end application.
