WEBVTT

00:01.610 --> 00:02.150
Okay.

00:02.150 --> 00:07.250
In this video, we will be talking about relationship in databases.

00:07.250 --> 00:11.540
So I will open models file here.

00:12.590 --> 00:20.540
And so far we have just one class, one model in our database, which is one table, and we will add

00:20.540 --> 00:22.810
more into our databases.

00:22.820 --> 00:29.510
We'll create new models, and I will show you how to build a relationship in between the models.

00:29.510 --> 00:31.850
So what kind of relationship we have?

00:31.880 --> 00:35.870
We have 1 to 1, one to many and many to many.

00:36.200 --> 00:43.940
And I will show you different examples where we might use different relationship type and how we can

00:43.940 --> 00:44.780
use it.

00:44.780 --> 00:53.000
So in this video, we will be doing another model that will be 1 to 1 with our book.

00:53.120 --> 00:56.500
So what will be the use case of 1 to 1?

00:56.510 --> 01:06.020
Usually when we want to have 1 to 1 is that we will have two records in our database and for each record,

01:06.020 --> 01:13.830
the one record in our example book will have just only one record of something else.

01:14.340 --> 01:22.830
So we can have multiple books, we can have multiple another objects, but each of these will have only

01:22.830 --> 01:24.540
1 to 1 relationship.

01:24.570 --> 01:31.260
Usually this kind of relationship is used to extend our model somehow.

01:31.260 --> 01:36.660
So we have basic information in our book and then we extend it with something extra.

01:36.660 --> 01:40.830
And that extension is only for that specific book.

01:40.830 --> 01:44.060
So in our case, we can represent it.

01:44.070 --> 01:47.610
I can show it using our example of book number.

01:47.610 --> 01:54.990
So let's create class book number and I will explain it in a second.

01:54.990 --> 01:59.550
So we'll do the same thing as we've done above and then model.

02:01.350 --> 02:03.900
So we have another book number here.

02:03.900 --> 02:13.320
So each book has its own ISBN, which is internal international standard book number, and that number

02:13.320 --> 02:18.840
is unique and it's given to each book that has been published.

02:18.840 --> 02:21.010
So we have two type of ISBN.

02:21.250 --> 02:22.530
ISBN.

02:23.790 --> 02:25.980
Then we can call it ten.

02:26.610 --> 02:31.980
So it will be models char field.

02:33.790 --> 02:36.430
And let's max length will be ten.

02:37.840 --> 02:42.100
And also we can do, uh, that's supposed to be ISBN.

02:43.370 --> 02:45.410
And then I will duplicate it.

02:46.640 --> 02:54.560
And another type of ISBN, I think they back in the days they had ISBN ten, which is ten characters

02:54.560 --> 03:01.040
long a string to uniquely identify the book number.

03:01.040 --> 03:07.550
But now it's ISBN at 13, so all the others will have both ten and 13.

03:07.940 --> 03:10.280
Anyway, we'll have this.

03:11.240 --> 03:20.030
So we'll have one field which is just a char field and another 113 so we can do a here.

03:20.030 --> 03:20.720
Blank.

03:21.800 --> 03:22.670
True.

03:26.060 --> 03:29.300
And we can also hear blank True.

03:30.200 --> 03:32.780
So we allow this to be blank.

03:32.780 --> 03:36.560
So we have another model registered here.

03:37.010 --> 03:37.790
At the moment.

03:37.790 --> 03:41.570
We have no relationship in between them.

03:41.930 --> 03:44.540
So how can we create a relationship?

03:44.990 --> 03:46.870
We need to create our field.

03:46.880 --> 03:50.090
In our case it will create on a book site.

03:50.720 --> 04:00.470
So we will do, let's call it number and then we'll do models and 1 to 1 field.

04:01.630 --> 04:02.260
Here.

04:02.260 --> 04:04.690
We need to provide the class name.

04:05.260 --> 04:07.590
So first argument is the class name.

04:07.600 --> 04:11.230
So 1 to 1 field and we will use the book number.

04:11.230 --> 04:14.710
And I will tell you in a second why this is underscored red.

04:14.920 --> 04:20.680
So what we need to do also we can decide that null is true.

04:23.680 --> 04:27.550
So by it will be stored as null.

04:28.420 --> 04:30.700
And then we can also say blank.

04:30.700 --> 04:31.300
True.

04:31.330 --> 04:33.190
So it's not required.

04:33.220 --> 04:35.170
Also, we need to do.

04:36.840 --> 04:49.650
On the lead models cascade and we can move it to another so I can do it here.

04:49.980 --> 04:56.220
So we decided that if that will be empty, we'll store it as null, then it's not required.

04:56.220 --> 04:58.950
And also on delete, we'll do models cascade.

04:58.980 --> 05:04.620
That means when we delete one another will be cascading and in being deleted as well.

05:04.620 --> 05:08.900
So we can see here this is underscore red for a reason.

05:08.910 --> 05:13.260
The way the python reads a file is from the top to the bottom.

05:13.260 --> 05:21.870
So when it reached the 15 line here, there is no way he can tell what the book number is because the

05:21.870 --> 05:26.730
book number is only available from line 21.

05:26.940 --> 05:33.150
And if I will take this and I will just cut it here and paste it there.

05:35.050 --> 05:37.690
You can see there is no errors now.

05:38.410 --> 05:40.330
And that's very important.

05:40.330 --> 05:46.570
When you design your models, you need to think about it, how to design it in order.

05:46.570 --> 05:49.360
They will be available in the file.

05:50.050 --> 05:57.100
So if you have multiple models, you need to design it the way that you don't want to do a cross reference

05:57.100 --> 06:05.320
because you might have a situation that something will needs to be available in one model but also needs

06:05.320 --> 06:06.970
to be available on another model.

06:06.970 --> 06:11.080
And the way you order the things really matters.

06:11.350 --> 06:14.650
So just pay attention to to the order.

06:14.650 --> 06:20.740
So we have our book, we have our 1 to 1 field, to the book number, which is this one.

06:21.070 --> 06:24.430
And then we decided what to do with the data.

06:24.430 --> 06:36.350
So as always, what we need to do in the terminal, we do Python manage.py make migrations.

06:37.900 --> 06:40.370
And you can see here two things happened.

06:40.390 --> 06:46.450
First, we create a new model, which is new table in our database, and then we add field number to

06:46.450 --> 06:46.780
the book.

06:46.780 --> 06:51.280
So we have that number and that's a 1 to 1 reference to our book number.

06:51.280 --> 06:53.740
And then we need to also do migrate.

06:58.020 --> 07:03.130
And that has been migrated to our database and it's available for us to use.

07:03.150 --> 07:08.670
So what we also can do is if I will go to run

07:12.210 --> 07:16.710
admin, you can see here we only see books here.

07:17.250 --> 07:24.270
So what we need to do is we need to go to admin and we can register our new model.

07:44.000 --> 07:45.440
We've done that before.

07:49.230 --> 07:57.270
So we import book number and then we do admin size register and the book here if I'll come back to this.

07:58.290 --> 07:59.580
Page and refresh.

07:59.610 --> 08:03.720
You can see book number is automatically registered there.

08:04.550 --> 08:06.050
And I can add it or change it.

08:06.050 --> 08:14.030
So what we can do is, if I will add here, I have that ISBN ten and ISBN 13 available for us and we

08:14.030 --> 08:16.760
can save it, but I don't want to do it here.

08:16.760 --> 08:24.140
What I want to do is I will show you if we go home and if we go to the books and this is how it works.

08:24.140 --> 08:32.000
So if we go to The Hobbit, you can see this is 1 to 1 field here, the number because we call it number.

08:32.000 --> 08:35.480
So if I click on this, nothing is there.

08:35.510 --> 08:43.040
That's a dropdown from all available book numbers that are available in our database, but in fact we

08:43.040 --> 08:44.960
don't have any yet.

08:44.960 --> 08:49.190
So we can do we can directly add it from here.

08:49.190 --> 08:51.650
So let's say we have.

08:52.990 --> 08:53.650
Some.

08:53.650 --> 08:55.360
One, two, three, four, five.

08:56.350 --> 08:59.710
Some one, two, three, four, five, six, seven, eight.

08:59.740 --> 09:00.730
Whatever that is.

09:00.730 --> 09:02.020
That's supposed to be ten.

09:02.050 --> 09:02.820
That's 13.

09:02.830 --> 09:03.880
It doesn't really matter.

09:03.880 --> 09:09.460
So if we save it, you can see book number here is assigned to this book.

09:09.460 --> 09:15.850
So if I will have that ISBN, real ISBN, I can assign them to this specific book.

09:15.850 --> 09:17.320
So I will save it here.

09:17.740 --> 09:19.240
And that's been added.

09:19.240 --> 09:22.090
So this hobbit has that.

09:22.660 --> 09:32.080
If I go if I go back here to the books and you can see lots of the ring here, if I will try to pick

09:32.080 --> 09:38.620
something like that, which is the same book number I've created for a Hobbit and save it, You can

09:38.620 --> 09:42.610
see there is an error book with this number already exist.

09:42.610 --> 09:45.580
And the reason I see that error is.

09:47.220 --> 09:49.470
This specific thing.

09:49.470 --> 09:54.560
So 1 to 1 can only have one per book.

09:54.570 --> 10:02.820
So if you will try to assign a the same one to the book, it will throw an error like this.

10:02.820 --> 10:09.030
So if you want to see that book, you need to add a new one with new ISBNs.

10:09.600 --> 10:12.360
Okay, so we have that sorted out.

10:12.360 --> 10:16.560
Let's come back to the postman and see what we will see here.

10:16.560 --> 10:21.750
So we have our books here, list of books, and now we've added extra fields.

10:21.750 --> 10:26.070
So what we can do here is test it and nothing is there.

10:26.070 --> 10:32.160
So the reason for that is because we haven't add anything in serializer.

10:32.160 --> 10:36.210
So we have ID title description price published and is published.

10:36.210 --> 10:48.460
So if I will take a new field we have created and put it here on Serializers, that's like that if I

10:48.490 --> 10:49.630
refresh it now.

10:51.820 --> 10:54.400
Going back to Postman and then.

10:55.120 --> 10:59.620
You can see here, number one and number null.

10:59.620 --> 11:01.000
And what is that?

11:01.240 --> 11:06.610
Well, basically, that's ID of our book number reference in our database.

11:06.610 --> 11:11.290
And this is Null because we haven't added anything to Lords of the Ring.

11:11.290 --> 11:12.460
And this is one.

11:12.460 --> 11:19.090
Well that's not really useful for us that much because if we have ISBN, we'd like to see ISBN here.

11:19.090 --> 11:30.940
So what we can do is we can copy entire serializer, paste it here and what I will do is I will have

11:30.940 --> 11:34.900
another serializer, create a new one number.

11:35.050 --> 11:37.360
So we'll have book number serializer.

11:37.360 --> 11:39.370
It will use the model Serializer.

11:39.370 --> 11:42.940
In our case, it's a book number.

11:42.970 --> 11:58.270
We need to also import it here and then inside we will have ID, then we have ISBN, then an ISBN 13.

11:58.300 --> 12:02.350
That's what we have available on our model like that.

12:02.350 --> 12:07.960
So we have another serializer here and we have number here.

12:07.960 --> 12:11.320
So what we can do in the serializer, we can connect them.

12:11.320 --> 12:27.550
So I can say number is our book number serializer like that, and then I can provide many faults.

12:29.110 --> 12:33.220
So what I'm saying, this will be always one record, not an array.

12:33.220 --> 12:35.620
So many, many faults.

12:35.620 --> 12:45.820
And then instead of a simple ID for for this record in a database, we'll use the whole serializer and

12:45.820 --> 12:47.920
that whole serializer will give us ID.

12:48.250 --> 12:49.120
ISBN.

12:49.150 --> 12:52.630
ISBN So let's test it now.

12:52.630 --> 12:58.310
I will save it and come back to Postman and we send it again.

12:58.310 --> 13:04.910
And you can see here now, number is not just an ID, it's a whole object inside object.

13:04.940 --> 13:07.460
This one is null, so nothing is there.

13:07.460 --> 13:12.560
But this is what we have added a moment ago in our admin page.

13:12.560 --> 13:17.000
So you can see that some and something here along with ID.

13:17.300 --> 13:24.140
So this is the way to have nested serializers one inside another.

13:24.140 --> 13:28.670
And then you can do inside this, you can have another nested serializer.

13:28.670 --> 13:32.210
So we can keep on going and going with this.

13:32.480 --> 13:39.380
And I also show you how we can actually create another model and how we can connect with them so we

13:39.380 --> 13:43.280
know how to do, how to do 1 to 1 in the next video.

13:43.280 --> 13:45.800
We will I will show you how to do one to many.
