WEBVTT

00:02.220 --> 00:02.760
Okay.

00:02.940 --> 00:07.110
We know already how to do 1 to 1 relationship in our database.

00:07.140 --> 00:11.100
Let's do a one to many relationship now.

00:11.100 --> 00:13.260
So let's go back to the models here.

00:13.380 --> 00:19.070
And we have the previous setup as we finish in the last video.

00:19.080 --> 00:25.860
So the example we can use for a one to many will be characters in a book.

00:25.860 --> 00:31.130
So each book has a characters in a books.

00:31.140 --> 00:40.710
And for example, in the in The Hobbit, we might have a Frodo or any other character and we can register

00:40.710 --> 00:42.900
that in in our database.

00:42.900 --> 00:50.190
But one book might have and probably will have more than one, like in the example of the book number,

00:50.190 --> 00:51.840
that's only 1 to 1.

00:51.840 --> 00:55.020
But book will have many characters.

00:55.020 --> 01:04.420
So instead of putting that field on the book, we can create another model and we can reference that

01:04.420 --> 01:05.890
model to the book.

01:05.890 --> 01:08.290
So let's see how we can do that.

01:09.010 --> 01:21.610
I will do character, which is just a book character, so models and then model, same as we've done

01:21.610 --> 01:22.240
before.

01:22.720 --> 01:33.070
So we can have character, let's say name and then models and char field.

01:35.800 --> 01:40.120
And we can do a max length, let's say 30.

01:40.630 --> 01:46.270
It doesn't really matter that much, but we'll have that character we might have here more, I don't

01:46.270 --> 01:46.570
know.

01:46.570 --> 01:55.150
H or whatever we need to store in this, but now we need to connect this character with this model.

01:55.150 --> 02:03.790
As I said, we might have a lot of characters so we can say book and that will be a reference to our

02:03.790 --> 02:04.330
book.

02:04.330 --> 02:09.190
And then we do models and then we use foreign key.

02:09.430 --> 02:13.600
Foreign key is a connection between this and this.

02:13.600 --> 02:25.150
So we need to also pass this book class name here and then we do the same as we've done on delete and

02:25.150 --> 02:30.010
then models cascade like that.

02:33.010 --> 02:35.140
So I will leave it like that.

02:35.140 --> 02:35.750
Now.

02:35.750 --> 02:43.760
And we have another character so we can create new characters and we can assign them to a specific book

02:43.790 --> 02:44.780
here.

02:44.780 --> 02:46.640
So I will save it now.

02:46.850 --> 02:49.010
And let's go back to terminal.

02:49.010 --> 02:57.200
We'll do our up twice, make migrations, arrow up, migrate and then that's been applied.

02:57.200 --> 03:05.510
So let's go back to the admin here and I will just duplicate it here and then add this class name.

03:05.930 --> 03:15.020
I will also need to import and register here so we have that character register in our admin section.

03:15.020 --> 03:20.690
So let's come back here and we can go home and we have our characters here.

03:20.690 --> 03:28.460
So let's add a character here so we can do Frodo and then we can decide it.

03:28.460 --> 03:32.060
Oh, that's Frodo is in The Hobbit, so I will save it.

03:32.600 --> 03:34.520
And this has been added.

03:34.520 --> 03:36.470
What I can do is I can.

03:37.830 --> 03:38.490
Added.

03:39.190 --> 03:42.880
Actually, I don't think Frodo was in The Hobbit anyway.

03:43.000 --> 03:49.690
Bilbo and I can also decided to to apply that in The Hobbit.

03:50.980 --> 03:52.600
So let's correct the first one.

03:52.600 --> 03:57.760
Frodo wasn't there, so let's do Gandalf.

04:03.860 --> 04:08.000
So we have one Gandalf and one Hob, one Bilbo.

04:08.090 --> 04:10.970
And both of them are in the book.

04:11.390 --> 04:16.310
So we have two characters in the book in The Hobbit here.

04:16.310 --> 04:21.860
But as you can see here, nothing is available or visible, visible here.

04:21.890 --> 04:29.810
The reason for that is because we actually have the reference to our book from the characters, not

04:29.810 --> 04:32.870
from the from the book itself.

04:32.870 --> 04:41.540
But if we go to the serializers here now and I will just create another serializer, I duplicate this.

04:41.540 --> 04:45.350
So this time we do character.

04:49.470 --> 04:51.540
Also we need to import it

04:54.900 --> 04:58.980
and use it here and we have just name.

05:00.180 --> 05:02.790
So we have that character here.

05:03.120 --> 05:12.240
But I can't really use the character here on the Book Serializer because I have no reference from this

05:12.240 --> 05:12.860
book.

05:12.870 --> 05:20.400
So what we can do is I will show you if I will use the similar style as I've used here character.

05:21.180 --> 05:28.860
And then let's include that here, uh, like that.

05:28.860 --> 05:34.230
And then we'll use the cargo character serializer.

05:34.230 --> 05:37.830
And in this case, I will say many true.

05:38.340 --> 05:43.320
So what I'm doing here, I have a new serializer.

05:43.350 --> 05:45.660
That's for a new model I have created.

05:45.660 --> 05:50.170
And then I have a new variable here, which is character.

05:50.170 --> 05:51.730
And then this.

05:51.730 --> 05:58.840
Actually, it should be characters like that and it will use the character serializer and will pass

05:58.840 --> 05:59.980
it many through.

05:59.980 --> 06:05.830
And then this characters are also included in our fields there.

06:06.070 --> 06:10.420
But as I said, we don't have that available on the book Serializer.

06:10.420 --> 06:17.800
So if I will go now here and send it, you will see we'll get an error because the characters is not

06:17.800 --> 06:19.090
available on the book.

06:19.090 --> 06:25.510
So what we can do is we go to the models and we don't add it here like 1 to 1.

06:25.510 --> 06:32.500
But what we can do is on the book with the foreign key, foreign key on that field, what we can do

06:32.500 --> 06:42.340
is we can do related name and in the related name I will put it on another line related name.

06:42.340 --> 06:44.470
We can pass that name.

06:44.470 --> 06:46.150
So if we save it now.

06:48.110 --> 06:51.200
We can come back here and refresh it.

06:51.350 --> 06:54.860
And in this case, we can see characters here.

06:54.860 --> 06:59.980
That's an array in this case because we specify in the serializer it's many.

06:59.990 --> 07:00.760
True.

07:00.770 --> 07:09.560
So the in the serializer here we specify many true for the characters and many false for the book number.

07:09.590 --> 07:17.360
So coming back here, you can see the number is just a simple object and the characters.

07:17.360 --> 07:20.210
It's because many true it's an array.

07:20.540 --> 07:26.030
And inside we have one object, which is Gandalf and another object which is Bilbo.

07:26.240 --> 07:30.680
And in the case of another book, we don't have any characters.

07:30.680 --> 07:38.000
So you can clearly see here that 1 to 1, just one object per book, and this is our array.

07:38.030 --> 07:45.230
So we might have many objects inside for for that specific book.

07:45.260 --> 07:52.860
So this is how we can create a one to many relationship in our databases.

07:52.860 --> 07:58.740
And we for that we use a foreign key, as you can see here.

07:58.740 --> 08:00.720
So this is the way to connect.

08:00.720 --> 08:05.520
Also, you need to make sure that this book is registered before.

08:05.790 --> 08:10.050
So you need to remember the order of them.

08:10.050 --> 08:15.030
And you can see here you can create as many fields as you like.

08:15.030 --> 08:17.760
You don't you are not restricted to only one.

08:17.760 --> 08:24.300
So it might be another big model and you can connect with them using foreign key.
