WEBVTT

00:00.870 --> 00:01.440
Okay.

00:01.560 --> 00:06.430
We know how to create 1 to 1 and one to many relationship in databases.

00:06.450 --> 00:09.150
Let's do a many to many.

00:09.150 --> 00:10.360
So many to many.

00:10.380 --> 00:17.100
That means we'll have a set of records on one side and a set of records on another side, and we might

00:17.100 --> 00:18.480
have multiple.

00:19.290 --> 00:21.120
Connections in between them.

00:21.120 --> 00:27.380
So in our example that we use book, the perfect example will be author.

00:27.390 --> 00:36.570
So author might have a lot of books, so books will be written by one author, author, and that author

00:36.570 --> 00:38.820
might have more than one book.

00:38.820 --> 00:40.980
So we can create that.

00:43.640 --> 00:44.390
Class.

00:46.320 --> 00:47.100
Outer.

00:48.150 --> 00:55.950
And the same way models model, just this is just another model and then we can say name.

00:57.470 --> 00:58.520
Let's actually.

01:02.790 --> 01:03.600
Copy that.

01:03.630 --> 01:07.930
We are not really interested in these models to have them really precise.

01:07.950 --> 01:11.580
We just want to demonstrate what what is happening.

01:11.580 --> 01:16.350
So what we can have is we can have maybe name and surname here.

01:16.350 --> 01:17.070
So.

01:20.800 --> 01:23.560
Surname and then we'll have it like that.

01:23.560 --> 01:32.620
So to create a many to many, we'll need to have our reference in between the author and the book.

01:34.560 --> 01:37.590
We can add another field books.

01:40.060 --> 01:45.580
Models and then we can have many to many field.

01:47.040 --> 01:48.600
Then we need to decide.

01:48.600 --> 01:49.350
What.

01:50.160 --> 01:55.140
Class will use their what model?

01:55.140 --> 01:56.910
And I will use book.

01:57.940 --> 01:58.750
Like that.

01:59.050 --> 02:09.610
So once we have that added here, what we also need to do is I will first do my migrations and then

02:09.610 --> 02:10.480
migrate.

02:11.740 --> 02:13.120
So we have that.

02:13.120 --> 02:24.400
Now we go to admin and then we will register another model, which is our author.

02:27.310 --> 02:28.600
We need to import it.

02:30.580 --> 02:35.110
And the server is running so we can go here.

02:35.980 --> 02:42.670
And we have our outers here so we don't have any outer in the database.

02:42.670 --> 02:44.170
So let's create one.

02:44.770 --> 02:50.140
So j r r token.

02:50.140 --> 02:54.910
And you can see here the user interface is different for many to many.

02:54.910 --> 02:59.950
I can actually pick many of them here or I can add another one here.

02:59.950 --> 03:06.070
So this outer is actually will have a relationship with this book and with this book.

03:06.070 --> 03:07.570
So I will save it here.

03:08.020 --> 03:12.760
And we have one outer that will be in this.

03:14.510 --> 03:16.610
And then we can save it here.

03:16.940 --> 03:20.210
So we have our outer object there.

03:22.460 --> 03:27.380
And we can add it also in our serializer.

03:27.380 --> 03:29.020
So let's come back here.

03:29.030 --> 03:32.270
We have our character Serializer.

03:32.490 --> 03:39.620
I'll duplicate it here and then we'll do our outer outer serializer.

03:39.830 --> 03:47.930
Also, we need to use outer here and import it from from the top, from the model.

03:47.930 --> 03:52.310
And we can use ID, we can use name.

03:52.310 --> 03:56.540
And also we had a surname surname there.

03:56.990 --> 04:02.930
So we have outer serializer and we need to do a similar thing, what we've done with our characters

04:02.930 --> 04:06.530
basically that will be outdoors.

04:19.320 --> 04:21.410
And then we need to do outdoors.

04:22.800 --> 04:23.880
Add it here.

04:24.690 --> 04:29.940
So outdoors and then coming back to our postman, I will send it.

04:30.060 --> 04:35.270
And you can see we are having the same kind of error we had before.

04:35.280 --> 04:37.140
We already know how to solve it.

04:37.140 --> 04:42.150
So we go back to the models and then we do related.

04:43.420 --> 04:44.920
Related name.

04:45.760 --> 04:48.610
And then we can do ours.

04:49.360 --> 04:50.920
So I save it again.

04:51.040 --> 04:53.260
Go on, postman, refresh it.

04:53.260 --> 04:57.970
And you can see here we have different relationships.

04:57.970 --> 05:04.000
So we have 1 to 1, one to many and many to many.

05:04.000 --> 05:06.040
And the many to many.

05:06.040 --> 05:07.540
Here is.

05:08.250 --> 05:15.750
You have this one juror talking here and also you have the same author here.

05:15.930 --> 05:20.300
So you have authors this this one and this one.

05:20.310 --> 05:26.700
It's the same record in database, but you can see it is available on both of them.

05:26.700 --> 05:28.900
And then we have characters here.

05:28.920 --> 05:31.140
You might have more, more than one.

05:31.140 --> 05:34.890
And there will be specific for for this book.

05:34.890 --> 05:38.940
And also you have a simple.

05:39.630 --> 05:41.750
1 to 1 relationship.

05:41.760 --> 05:49.410
So that's how the relationship in Django works and that's generally all what it is to it.

05:49.710 --> 05:50.020
A.

05:51.210 --> 05:55.470
When you design your application, it's very important to think it through.

05:55.500 --> 05:57.270
What would you like to store it in?

05:57.270 --> 05:58.650
What models?

05:58.650 --> 06:03.750
How would would you like to use it and what will be the relationship in between them?

06:03.750 --> 06:11.370
Because the way you construct the A models and the serializes later on it will matter for you how you

06:11.370 --> 06:14.240
will receive the data from your database.

06:14.250 --> 06:20.070
So as you can see here, we using the serializer, we can build the very complex objects.

06:20.070 --> 06:28.260
So we are asking for books, but we get the array not for this book specific book, not just the book

06:29.010 --> 06:34.680
ID, but we also have another model, which is then another model.

06:34.680 --> 06:40.260
And we can actually include here more information than just a simple ID and name.

06:40.830 --> 06:44.070
And as you can see here, we can decide on any field that we store it.

06:44.070 --> 06:50.070
We try to keep our model very simple, but in fact, you can in the real application, they are not

06:50.070 --> 06:50.550
simple.

06:50.550 --> 06:55.180
They are full of data, real data, and they're real useful.

06:55.180 --> 07:01.690
And it's really up to you which fields you would like to include it and how you would like to structure.

07:01.690 --> 07:09.800
The one thing I haven't said yet is you don't need to have one serializer per model.

07:09.820 --> 07:16.780
So if you go to the serializer here, what you can do is you can possibly create another serializer

07:16.780 --> 07:20.350
for a book, for for a book.

07:20.380 --> 07:25.300
And you will exclude include only, for example, ID and title.

07:25.300 --> 07:28.270
And that's completely I can actually show you.

07:28.270 --> 07:33.900
So if I will copy that and then I will remove all of that data.

07:33.910 --> 07:36.010
So we'll still have a book serializer.

07:36.010 --> 07:39.340
So book mini serializer, I will name it.

07:39.340 --> 07:47.020
The name needs to be unique, but you can use the same model, so I will just keep it ID and title.

07:48.050 --> 07:50.000
So I can have it like that.

07:50.000 --> 07:52.780
And then I have this serializer.

07:52.790 --> 07:55.160
I will copy the serializer.

07:55.160 --> 08:03.950
And if I will go to the views and I have my view set, I in this view set, I use serializer class book

08:03.950 --> 08:04.640
serializer.

08:04.640 --> 08:08.120
So let's say I want to use book mini serializer.

08:08.360 --> 08:14.450
I will import that also from the serializer and I will show you that in a second how it works.

08:14.450 --> 08:19.160
So postman, if I will send it now and let's come back here.

08:19.160 --> 08:21.110
I have book mini serializer.

08:21.990 --> 08:24.630
The Serializers have been saved.

08:35.790 --> 08:39.390
And this will go here.

08:40.470 --> 08:42.270
I'll rerun the server.

08:44.810 --> 08:49.470
And you can see here I have that mini serializer in use.

08:49.490 --> 08:58.010
So what is that doing is by default, it will use that mini serializer because I decided to use it here,

08:58.010 --> 08:59.060
in my view, set.

08:59.060 --> 09:03.980
But inside here, what I can do, I will show you a little trick.

09:03.980 --> 09:08.000
If I'll go click command and click on the models here.

09:08.000 --> 09:14.450
I have different methods that are included in the view set and you can see here we have create, retrieve,

09:14.480 --> 09:17.350
update, partial update, destroy and list.

09:17.360 --> 09:23.270
So basically what we are seeing here and when we go.

09:24.480 --> 09:25.170
Here.

09:25.170 --> 09:28.800
That's a list because it's a list of all our records here.

09:28.800 --> 09:29.580
But.

09:30.840 --> 09:31.710
Also.

09:33.190 --> 09:34.900
We have retrieved one.

09:34.900 --> 09:38.680
So retrieve is just retrieving one single.

09:39.550 --> 09:42.250
I will copy that from this is Django code.

09:42.250 --> 09:44.800
I will copy that here and I will come back to the view.

09:44.800 --> 09:49.720
So we have that retrieve method available in the view set.

09:49.720 --> 09:53.710
But if I will include it here, that means I would like to use my own.

09:53.710 --> 09:56.770
I just copy the one from the from the view set.

09:56.770 --> 09:59.980
So it is at this point it is the same.

09:59.980 --> 10:04.000
So you can see here response is not imported.

10:04.000 --> 10:05.890
So we need to import response here.

10:09.020 --> 10:11.030
From rest.

10:12.270 --> 10:13.830
Rest framework.

10:15.940 --> 10:16.730
Response.

10:16.750 --> 10:19.630
Import response like that.

10:19.930 --> 10:25.810
And here we have get serializer, which is this serializer class.

10:25.810 --> 10:26.830
We will get it.

10:26.830 --> 10:31.090
But instead of that, I can use the serializer.

10:31.090 --> 10:33.700
We had the full serializer.

10:33.700 --> 10:34.840
We had it here.

10:34.840 --> 10:37.240
So I will go back to the view set.

10:37.480 --> 10:42.910
I'm kind of doing that very fast now, but I will explain it once again.

10:43.730 --> 10:46.490
In a second, so we'll go serializer.

10:48.390 --> 10:49.260
Like that.

10:50.830 --> 10:55.240
So what I am doing here is for method retrieve.

10:55.270 --> 10:59.020
I will use the same method as it was built in there.

10:59.260 --> 11:02.860
And for this one I would like to use the book serializer.

11:02.860 --> 11:08.050
So the default one, the serializer class for an entire view set is the mini one.

11:08.680 --> 11:11.290
Which contains only ID and title.

11:11.290 --> 11:16.230
And as you can see here, so we have that mini one.

11:16.240 --> 11:24.940
But if we will use the retrieve, which is one specific record from database, we will use the book

11:24.940 --> 11:28.360
serializer, which is the full serializer for all the data.

11:28.360 --> 11:29.380
So let's test it.

11:29.380 --> 11:34.690
Now we go to the postman and I will send it again and see here.

11:34.690 --> 11:41.860
It's just a mini serializer shown, but if I will go book ID one, which is hobbit and then I will send

11:41.860 --> 11:42.340
it.

11:42.640 --> 11:46.360
You can see I have all the data available for this.

11:46.360 --> 11:51.040
So if you would like to optimize your database queries.

11:51.040 --> 11:55.360
And for example, we have just a page with a title on it.

11:55.390 --> 12:01.990
What you can do is you can have mini serializer for this because that will what you will need on your

12:02.560 --> 12:03.310
page.

12:03.310 --> 12:10.730
But if someone will click on this ID, you will take this ID from here and you will pass it to your

12:10.730 --> 12:16.910
URL like that, and then you will load it and you will have all the data available for this book.

12:16.940 --> 12:22.640
In that way, you don't need to have all the data for all books in your system.

12:22.640 --> 12:29.420
You will just ask for the data you will need for a certain part of your application and that's very

12:29.420 --> 12:30.050
optimized.

12:30.050 --> 12:31.910
You don't overload it with the data.

12:31.910 --> 12:32.510
You will.

12:32.510 --> 12:36.080
You might never use it and that's how you can create it.

12:36.080 --> 12:45.590
So doing that again in the serializers you can actually have more than one serializer for the same class.

12:45.590 --> 12:50.690
You might decide which one will have how many fields here and.

12:52.120 --> 12:55.030
This is the way we can actually decide it.

12:55.030 --> 13:02.410
For each method that are available in the model view that we can override it and we can decide it.

13:02.410 --> 13:07.480
What serializer will be used for that certain one and that will be the default one.

13:08.450 --> 13:09.110
Okay.

13:09.530 --> 13:14.180
And that's about it for our Django in the introduction.

13:15.080 --> 13:23.720
I hope at this point you kind of understand how it all work and we are kind of ready to start building

13:23.720 --> 13:27.740
our API and that will be our first project.

13:27.860 --> 13:33.080
So we'll go again from the beginning and we'll create our mini application.

13:33.080 --> 13:35.330
That will be our backend API.
