Okay.
We know already how to do 1 to 1 relationship in our database.
Let's do a one to many relationship now.
So let's go back to the models here.
And we have the previous setup as we finish in the last video.
So the example we can use for a one to many will be characters in a book.
So each book has a characters in a books.
And for example, in the in The Hobbit, we might have a Frodo or any other character and we can register
that in in our database.
But one book might have and probably will have more than one, like in the example of the book number,
that's only 1 to 1.
But book will have many characters.
So instead of putting that field on the book, we can create another model and we can reference that
model to the book.
So let's see how we can do that.
I will do character, which is just a book character, so models and then model, same as we've done
before.
So we can have character, let's say name and then models and char field.
And we can do a max length, let's say 30.
It doesn't really matter that much, but we'll have that character we might have here more, I don't
know.
H or whatever we need to store in this, but now we need to connect this character with this model.
As I said, we might have a lot of characters so we can say book and that will be a reference to our
book.
And then we do models and then we use foreign key.
Foreign key is a connection between this and this.
So we need to also pass this book class name here and then we do the same as we've done on delete and
then models cascade like that.
So I will leave it like that.
Now.
And we have another character so we can create new characters and we can assign them to a specific book
here.
So I will save it now.
And let's go back to terminal.
We'll do our up twice, make migrations, arrow up, migrate and then that's been applied.
So let's go back to the admin here and I will just duplicate it here and then add this class name.
I will also need to import and register here so we have that character register in our admin section.
So let's come back here and we can go home and we have our characters here.
So let's add a character here so we can do Frodo and then we can decide it.
Oh, that's Frodo is in The Hobbit, so I will save it.
And this has been added.
What I can do is I can.
Added.
Actually, I don't think Frodo was in The Hobbit anyway.
Bilbo and I can also decided to to apply that in The Hobbit.
So let's correct the first one.
Frodo wasn't there, so let's do Gandalf.
So we have one Gandalf and one Hob, one Bilbo.
And both of them are in the book.
So we have two characters in the book in The Hobbit here.
But as you can see here, nothing is available or visible, visible here.
The reason for that is because we actually have the reference to our book from the characters, not
from the from the book itself.
But if we go to the serializers here now and I will just create another serializer, I duplicate this.
So this time we do character.
Also we need to import it
and use it here and we have just name.
So we have that character here.
But I can't really use the character here on the Book Serializer because I have no reference from this
book.
So what we can do is I will show you if I will use the similar style as I've used here character.
And then let's include that here, uh, like that.
And then we'll use the cargo character serializer.
And in this case, I will say many true.
So what I'm doing here, I have a new serializer.
That's for a new model I have created.
And then I have a new variable here, which is character.
And then this.
Actually, it should be characters like that and it will use the character serializer and will pass
it many through.
And then this characters are also included in our fields there.
But as I said, we don't have that available on the book Serializer.
So if I will go now here and send it, you will see we'll get an error because the characters is not
available on the book.
So what we can do is we go to the models and we don't add it here like 1 to 1.
But what we can do is on the book with the foreign key, foreign key on that field, what we can do
is we can do related name and in the related name I will put it on another line related name.
We can pass that name.
So if we save it now.
We can come back here and refresh it.
And in this case, we can see characters here.
That's an array in this case because we specify in the serializer it's many.
True.
So the in the serializer here we specify many true for the characters and many false for the book number.
So coming back here, you can see the number is just a simple object and the characters.
It's because many true it's an array.
And inside we have one object, which is Gandalf and another object which is Bilbo.
And in the case of another book, we don't have any characters.
So you can clearly see here that 1 to 1, just one object per book, and this is our array.
So we might have many objects inside for for that specific book.
So this is how we can create a one to many relationship in our databases.
And we for that we use a foreign key, as you can see here.
So this is the way to connect.
Also, you need to make sure that this book is registered before.
So you need to remember the order of them.
And you can see here you can create as many fields as you like.
You don't you are not restricted to only one.
So it might be another big model and you can connect with them using foreign key.