WEBVTT

00:01.130 --> 00:01.760
Okay.

00:01.760 --> 00:08.960
Once we have added our rest framework tool to our Django, let's use it now.

00:08.960 --> 00:12.740
So we'll close these settings because we won't need it anymore.

00:12.740 --> 00:18.680
And what I will do is I will open the demo and I will create a new file and that file I will name it.

00:18.680 --> 00:22.730
Serializers Serializers.

00:22.850 --> 00:32.480
PY So basically here we will store the serializers here and I will explain you later on what the serializer

00:32.480 --> 00:33.020
is.

00:33.020 --> 00:38.030
I think it will be easier, if you will, when to understand it when you see it.

00:38.030 --> 00:46.340
So first thing we need to do from rest framework that's available for us because we just installed it.

00:46.370 --> 00:49.550
Import serializers.

00:49.790 --> 00:54.320
So we have that imported and then we can create a new serializer here.

00:54.740 --> 00:58.310
And there is a few way that we can actually create a serializer.

00:58.310 --> 01:05.900
But I will do a modal serializer and that will ease for in my opinion, that's the easiest way and more

01:05.900 --> 01:07.340
efficient way to work.

01:07.400 --> 01:09.410
Then everything will be based on models.

01:09.410 --> 01:14.180
So you can nicely sync in views, models and serializers.

01:14.180 --> 01:17.900
So we'll create our class and then we name it.

01:17.900 --> 01:19.670
So what do we want to do?

01:19.670 --> 01:22.610
First we want to display the book.

01:22.610 --> 01:34.790
So Book Serializer, and then inside we can include some built in Serializers modal serializer like

01:34.790 --> 01:37.580
that class.

01:39.480 --> 01:41.610
Meta model.

01:42.750 --> 01:45.510
And then we'll pick what model we would like to use.

01:45.540 --> 01:50.610
Obviously for us it will be book, but I also will need to import.

01:50.610 --> 01:53.280
So we are in the same folder.

01:53.280 --> 02:00.240
So from models import book, so dot models import book and now the book is available.

02:00.240 --> 02:02.580
And then here we can decide.

02:06.090 --> 02:08.010
What fields will include.

02:08.010 --> 02:11.790
So let's start with title like that.

02:11.790 --> 02:20.400
So we've created our serializer and what we are doing here is we name our serializer, we pass a built

02:20.400 --> 02:28.500
in Django methods and then we are telling, okay, we will use our model book and then we would like

02:28.500 --> 02:30.900
to display title fields.

02:30.930 --> 02:32.790
That's it for our serializer.

02:32.790 --> 02:34.650
At the moment we don't use it anywhere.

02:34.650 --> 02:37.650
So let's go back to the views.

02:38.680 --> 02:44.860
And in the similar way we've used before, we can create to here another class.

02:44.860 --> 02:48.240
So we'll do class and we'll do a view set.

02:48.250 --> 02:52.330
So I will import view set from Django Rest framework.

02:52.810 --> 03:01.480
So from rest framework import view sets like that.

03:01.570 --> 03:08.530
So my class will be will be we will use similar conventions we use with serializer.

03:08.530 --> 03:21.730
So we'll do book view set like that and then we can pass view sets and then we can have model modal

03:21.790 --> 03:22.570
view set.

03:23.410 --> 03:32.230
So basically we are creating a built in view for our books that will use all power of Django and it

03:32.230 --> 03:36.820
will create the option for the Http method for us.

03:36.820 --> 03:38.390
And I will show you that in a second.

03:38.390 --> 03:46.250
So serializer class and this is place where we actually tell which serializer we would like to use.

03:46.250 --> 03:53.570
So we have one serializer, and I will use this serializer here, but we don't have it available yet

03:53.570 --> 03:54.200
in this file.

03:54.200 --> 04:01.820
So what we need to do is we need to import so from dot serializer dot because we are in the same folder

04:01.820 --> 04:07.010
import book serializer one this is here, then we can use it.

04:07.010 --> 04:13.850
So for now, just to omit some confusions, I will remove all of what we had before.

04:13.850 --> 04:14.930
I will need to also.

04:17.850 --> 04:23.460
Correct the URLs in a second, but this is basically what we have seen so far.

04:23.460 --> 04:28.230
So we have a imported view set and then we have our serializer from another file.

04:28.230 --> 04:37.260
And then we are telling that setting up a new view set and actually we have like that view set and then

04:37.260 --> 04:38.610
a serializer class.

04:38.610 --> 04:40.200
It's a book serializer.

04:40.710 --> 04:43.650
Also here we need to decide.

04:45.310 --> 04:48.430
What is the query set for this view?

04:48.460 --> 04:56.080
The query set is something that we've done before and that will be our book objects.

04:57.400 --> 05:05.350
And then all and this is we are telling if we ever use this view set, what query set we would like

05:05.350 --> 05:08.800
to use and basically we will do use all books.

05:08.830 --> 05:11.500
Also, this big book needs to be imported.

05:11.500 --> 05:12.610
So from.

05:13.620 --> 05:16.650
Models where we have our book import book.

05:16.680 --> 05:21.420
Now we have we don't have any errors and then we have our view set here.

05:21.870 --> 05:23.220
So I will save it.

05:23.250 --> 05:29.460
Now we have Serializer and that serializer will use our book model and display title.

05:29.460 --> 05:31.200
And then we have a view set.

05:31.230 --> 05:32.610
The way we had it before.

05:32.610 --> 05:35.520
It was a simple class or a function.

05:35.520 --> 05:42.800
This one we this time we use model view set and I will show you how powerful that is in a second.

05:42.810 --> 05:46.470
So we have this and then we need to go to the URLs.

05:46.860 --> 05:55.560
So we need to instead of having this function passed to the our URL patterns, we'll use a router.

05:56.580 --> 06:01.680
So let's import from rest framework import.

06:03.370 --> 06:07.960
Routers and then we can create a new router here.

06:08.410 --> 06:11.860
So router, we can name it as we like.

06:12.100 --> 06:17.310
And then routers, which is that what we have imported here?

06:17.320 --> 06:19.600
And then we will use default router.

06:20.770 --> 06:21.520
Like that.

06:21.520 --> 06:26.710
So we have a new variable, a router and using rest framework routers.

06:27.370 --> 06:32.710
So here we can do router and then register.

06:32.860 --> 06:37.680
And then what we can do is we can register a URL.

06:37.690 --> 06:40.450
In our case it will be books.

06:42.130 --> 06:46.630
And then we can pass a view set.

06:46.780 --> 06:53.260
So we have a book view set here and we will pass to our router like that.

06:53.290 --> 06:55.290
We also need to import it.

06:55.300 --> 06:56.710
So from.

06:59.050 --> 07:03.220
Dot views, import view said like that.

07:03.220 --> 07:08.000
And the last thing what we need to do is we need to include it in our path.

07:08.020 --> 07:14.560
So I will remove this so we'll have an empty path here and then we can do include.

07:14.590 --> 07:16.420
So include.

07:17.650 --> 07:22.960
This is gone already, so we need to import that as well.

07:23.330 --> 07:25.390
We don't need this anymore.

07:25.990 --> 07:31.240
And then we will include here our router URLs.

07:34.170 --> 07:34.950
Like that.

07:35.190 --> 07:39.120
So basically what's happening here is we have our path.

07:39.150 --> 07:45.930
Remember, this path is already inside our demo, so it will be URL slash demo and whatever.

07:45.930 --> 07:47.210
Here we'll have it.

07:47.220 --> 07:50.820
It will be accessible through from the router.

07:50.820 --> 07:55.920
So at the moment we have register only one model, which is books.

07:55.920 --> 08:02.210
That books will go here and then it will use all what's available on the view set.

08:02.220 --> 08:04.530
So let's run our server now.

08:10.130 --> 08:13.280
A this supposed to be as URLs.

08:14.300 --> 08:16.550
I will run the server again.

08:17.800 --> 08:19.690
And then we have our server running.

08:19.690 --> 08:22.000
So let's come back here.

08:22.590 --> 08:24.090
To our.

08:26.420 --> 08:26.900
Demo.

08:29.420 --> 08:31.190
And look at that.

08:31.220 --> 08:33.560
Now we have another view.

08:33.560 --> 08:38.570
And that view comes free with the Django rest framework, as you can see here.

08:38.570 --> 08:43.910
And that's kind of UI for managing our rest.

08:44.120 --> 08:47.870
So what we see here, we have some methods here.

08:47.870 --> 08:58.100
So basically if I will use this URL in some kind of API browser and I will show you later on how we

08:58.100 --> 09:01.310
can do that, then I will see a Json.

09:01.310 --> 09:09.380
So what we can do is I can click on this link and you can see here I'm seeing a list of our books.

09:10.010 --> 09:13.190
This is what we added in our database.

09:13.190 --> 09:21.560
And this here, this part, it's a Json, it's an array of objects and each object has a title and a

09:21.560 --> 09:22.160
value.

09:22.160 --> 09:26.870
So this is what we will use actually in our front end application.

09:26.870 --> 09:35.000
And the reason why you see title here and nothing else is because we added in the serializers here.

09:35.000 --> 09:42.380
So if I will do another field description and let's say price.

09:44.860 --> 09:45.430
I will save it.

09:45.430 --> 09:55.090
Now coming back here, refresh and you can see for each object here we have title description and price

09:55.210 --> 09:56.410
available for us.

09:56.410 --> 10:03.250
So in the serializers we decide what should be included in our object.

10:03.250 --> 10:06.610
So next time we will use a front end application.

10:06.610 --> 10:10.780
I would like to have a list of books from our backend.

10:10.810 --> 10:14.380
What we need to do is we need to use that URL.

10:14.380 --> 10:20.200
So our domain then demo, which is our application and then books.

10:20.200 --> 10:27.610
Books will register that as a view set in our router and then it will give us an array of objects like

10:27.610 --> 10:35.260
that and then we can do whatever we will we want we need to do in our front end application.

10:35.260 --> 10:38.950
So this is how it works and how the Django Rest framework works.

10:38.950 --> 10:42.190
This is just a handy another UI that we can use.

10:42.220 --> 10:45.650
And also you have another section for posting the data.

10:45.650 --> 10:54.410
And if I can, uh, go for example here books one and then slash, you can see here also I can update

10:54.410 --> 11:01.160
the data here and I will show you later on how we can actually use that different methods.

11:01.160 --> 11:07.160
So what's available for us is get put, patch, delete head options and so on.

11:07.160 --> 11:11.270
So this is typical methods for a rest API.
