Okay.
In this video, we will be talking about relationship in databases.
So I will open models file here.
And so far we have just one class, one model in our database, which is one table, and we will add
more into our databases.
We'll create new models, and I will show you how to build a relationship in between the models.
So what kind of relationship we have?
We have 1 to 1, one to many and many to many.
And I will show you different examples where we might use different relationship type and how we can
use it.
So in this video, we will be doing another model that will be 1 to 1 with our book.
So what will be the use case of 1 to 1?
Usually when we want to have 1 to 1 is that we will have two records in our database and for each record,
the one record in our example book will have just only one record of something else.
So we can have multiple books, we can have multiple another objects, but each of these will have only
1 to 1 relationship.
Usually this kind of relationship is used to extend our model somehow.
So we have basic information in our book and then we extend it with something extra.
And that extension is only for that specific book.
So in our case, we can represent it.
I can show it using our example of book number.
So let's create class book number and I will explain it in a second.
So we'll do the same thing as we've done above and then model.
So we have another book number here.
So each book has its own ISBN, which is internal international standard book number, and that number
is unique and it's given to each book that has been published.
So we have two type of ISBN.
ISBN.
Then we can call it ten.
So it will be models char field.
And let's max length will be ten.
And also we can do, uh, that's supposed to be ISBN.
And then I will duplicate it.
And another type of ISBN, I think they back in the days they had ISBN ten, which is ten characters
long a string to uniquely identify the book number.
But now it's ISBN at 13, so all the others will have both ten and 13.
Anyway, we'll have this.
So we'll have one field which is just a char field and another 113 so we can do a here.
Blank.
True.
And we can also hear blank True.
So we allow this to be blank.
So we have another model registered here.
At the moment.
We have no relationship in between them.
So how can we create a relationship?
We need to create our field.
In our case it will create on a book site.
So we will do, let's call it number and then we'll do models and 1 to 1 field.
Here.
We need to provide the class name.
So first argument is the class name.
So 1 to 1 field and we will use the book number.
And I will tell you in a second why this is underscored red.
So what we need to do also we can decide that null is true.
So by it will be stored as null.
And then we can also say blank.
True.
So it's not required.
Also, we need to do.
On the lead models cascade and we can move it to another so I can do it here.
So we decided that if that will be empty, we'll store it as null, then it's not required.
And also on delete, we'll do models cascade.
That means when we delete one another will be cascading and in being deleted as well.
So we can see here this is underscore red for a reason.
The way the python reads a file is from the top to the bottom.
So when it reached the 15 line here, there is no way he can tell what the book number is because the
book number is only available from line 21.
And if I will take this and I will just cut it here and paste it there.
You can see there is no errors now.
And that's very important.
When you design your models, you need to think about it, how to design it in order.
They will be available in the file.
So if you have multiple models, you need to design it the way that you don't want to do a cross reference
because you might have a situation that something will needs to be available in one model but also needs
to be available on another model.
And the way you order the things really matters.
So just pay attention to to the order.
So we have our book, we have our 1 to 1 field, to the book number, which is this one.
And then we decided what to do with the data.
So as always, what we need to do in the terminal, we do Python manage.py make migrations.
And you can see here two things happened.
First, we create a new model, which is new table in our database, and then we add field number to
the book.
So we have that number and that's a 1 to 1 reference to our book number.
And then we need to also do migrate.
And that has been migrated to our database and it's available for us to use.
So what we also can do is if I will go to run
admin, you can see here we only see books here.
So what we need to do is we need to go to admin and we can register our new model.
We've done that before.
So we import book number and then we do admin size register and the book here if I'll come back to this.
Page and refresh.
You can see book number is automatically registered there.
And I can add it or change it.
So what we can do is, if I will add here, I have that ISBN ten and ISBN 13 available for us and we
can save it, but I don't want to do it here.
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.
So if we go to The Hobbit, you can see this is 1 to 1 field here, the number because we call it number.
So if I click on this, nothing is there.
That's a dropdown from all available book numbers that are available in our database, but in fact we
don't have any yet.
So we can do we can directly add it from here.
So let's say we have.
Some.
One, two, three, four, five.
Some one, two, three, four, five, six, seven, eight.
Whatever that is.
That's supposed to be ten.
That's 13.
It doesn't really matter.
So if we save it, you can see book number here is assigned to this book.
So if I will have that ISBN, real ISBN, I can assign them to this specific book.
So I will save it here.
And that's been added.
So this hobbit has that.
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
something like that, which is the same book number I've created for a Hobbit and save it, You can
see there is an error book with this number already exist.
And the reason I see that error is.
This specific thing.
So 1 to 1 can only have one per book.
So if you will try to assign a the same one to the book, it will throw an error like this.
So if you want to see that book, you need to add a new one with new ISBNs.
Okay, so we have that sorted out.
Let's come back to the postman and see what we will see here.
So we have our books here, list of books, and now we've added extra fields.
So what we can do here is test it and nothing is there.
So the reason for that is because we haven't add anything in serializer.
So we have ID title description price published and is published.
So if I will take a new field we have created and put it here on Serializers, that's like that if I
refresh it now.
Going back to Postman and then.
You can see here, number one and number null.
And what is that?
Well, basically, that's ID of our book number reference in our database.
And this is Null because we haven't added anything to Lords of the Ring.
And this is one.
Well that's not really useful for us that much because if we have ISBN, we'd like to see ISBN here.
So what we can do is we can copy entire serializer, paste it here and what I will do is I will have
another serializer, create a new one number.
So we'll have book number serializer.
It will use the model Serializer.
In our case, it's a book number.
We need to also import it here and then inside we will have ID, then we have ISBN, then an ISBN 13.
That's what we have available on our model like that.
So we have another serializer here and we have number here.
So what we can do in the serializer, we can connect them.
So I can say number is our book number serializer like that, and then I can provide many faults.
So what I'm saying, this will be always one record, not an array.
So many, many faults.
And then instead of a simple ID for for this record in a database, we'll use the whole serializer and
that whole serializer will give us ID.
ISBN.
ISBN So let's test it now.
I will save it and come back to Postman and we send it again.
And you can see here now, number is not just an ID, it's a whole object inside object.
This one is null, so nothing is there.
But this is what we have added a moment ago in our admin page.
So you can see that some and something here along with ID.
So this is the way to have nested serializers one inside another.
And then you can do inside this, you can have another nested serializer.
So we can keep on going and going with this.
And I also show you how we can actually create another model and how we can connect with them so we
know how to do, how to do 1 to 1 in the next video.
We will I will show you how to do one to many.