Okay.
We know how to create 1 to 1 and one to many relationship in databases.
Let's do a many to many.
So many to many.
That means we'll have a set of records on one side and a set of records on another side, and we might
have multiple.
Connections in between them.
So in our example that we use book, the perfect example will be author.
So author might have a lot of books, so books will be written by one author, author, and that author
might have more than one book.
So we can create that.
Class.
Outer.
And the same way models model, just this is just another model and then we can say name.
Let's actually.
Copy that.
We are not really interested in these models to have them really precise.
We just want to demonstrate what what is happening.
So what we can have is we can have maybe name and surname here.
So.
Surname and then we'll have it like that.
So to create a many to many, we'll need to have our reference in between the author and the book.
We can add another field books.
Models and then we can have many to many field.
Then we need to decide.
What.
Class will use their what model?
And I will use book.
Like that.
So once we have that added here, what we also need to do is I will first do my migrations and then
migrate.
So we have that.
Now we go to admin and then we will register another model, which is our author.
We need to import it.
And the server is running so we can go here.
And we have our outers here so we don't have any outer in the database.
So let's create one.
So j r r token.
And you can see here the user interface is different for many to many.
I can actually pick many of them here or I can add another one here.
So this outer is actually will have a relationship with this book and with this book.
So I will save it here.
And we have one outer that will be in this.
And then we can save it here.
So we have our outer object there.
And we can add it also in our serializer.
So let's come back here.
We have our character Serializer.
I'll duplicate it here and then we'll do our outer outer serializer.
Also, we need to use outer here and import it from from the top, from the model.
And we can use ID, we can use name.
And also we had a surname surname there.
So we have outer serializer and we need to do a similar thing, what we've done with our characters
basically that will be outdoors.
And then we need to do outdoors.
Add it here.
So outdoors and then coming back to our postman, I will send it.
And you can see we are having the same kind of error we had before.
We already know how to solve it.
So we go back to the models and then we do related.
Related name.
And then we can do ours.
So I save it again.
Go on, postman, refresh it.
And you can see here we have different relationships.
So we have 1 to 1, one to many and many to many.
And the many to many.
Here is.
You have this one juror talking here and also you have the same author here.
So you have authors this this one and this one.
It's the same record in database, but you can see it is available on both of them.
And then we have characters here.
You might have more, more than one.
And there will be specific for for this book.
And also you have a simple.
1 to 1 relationship.
So that's how the relationship in Django works and that's generally all what it is to it.
A.
When you design your application, it's very important to think it through.
What would you like to store it in?
What models?
How would would you like to use it and what will be the relationship in between them?
Because the way you construct the A models and the serializes later on it will matter for you how you
will receive the data from your database.
So as you can see here, we using the serializer, we can build the very complex objects.
So we are asking for books, but we get the array not for this book specific book, not just the book
ID, but we also have another model, which is then another model.
And we can actually include here more information than just a simple ID and name.
And as you can see here, we can decide on any field that we store it.
We try to keep our model very simple, but in fact, you can in the real application, they are not
simple.
They are full of data, real data, and they're real useful.
And it's really up to you which fields you would like to include it and how you would like to structure.
The one thing I haven't said yet is you don't need to have one serializer per model.
So if you go to the serializer here, what you can do is you can possibly create another serializer
for a book, for for a book.
And you will exclude include only, for example, ID and title.
And that's completely I can actually show you.
So if I will copy that and then I will remove all of that data.
So we'll still have a book serializer.
So book mini serializer, I will name it.
The name needs to be unique, but you can use the same model, so I will just keep it ID and title.
So I can have it like that.
And then I have this serializer.
I will copy the serializer.
And if I will go to the views and I have my view set, I in this view set, I use serializer class book
serializer.
So let's say I want to use book mini serializer.
I will import that also from the serializer and I will show you that in a second how it works.
So postman, if I will send it now and let's come back here.
I have book mini serializer.
The Serializers have been saved.
And this will go here.
I'll rerun the server.
And you can see here I have that mini serializer in use.
So what is that doing is by default, it will use that mini serializer because I decided to use it here,
in my view, set.
But inside here, what I can do, I will show you a little trick.
If I'll go click command and click on the models here.
I have different methods that are included in the view set and you can see here we have create, retrieve,
update, partial update, destroy and list.
So basically what we are seeing here and when we go.
Here.
That's a list because it's a list of all our records here.
But.
Also.
We have retrieved one.
So retrieve is just retrieving one single.
I will copy that from this is Django code.
I will copy that here and I will come back to the view.
So we have that retrieve method available in the view set.
But if I will include it here, that means I would like to use my own.
I just copy the one from the from the view set.
So it is at this point it is the same.
So you can see here response is not imported.
So we need to import response here.
From rest.
Rest framework.
Response.
Import response like that.
And here we have get serializer, which is this serializer class.
We will get it.
But instead of that, I can use the serializer.
We had the full serializer.
We had it here.
So I will go back to the view set.
I'm kind of doing that very fast now, but I will explain it once again.
In a second, so we'll go serializer.
Like that.
So what I am doing here is for method retrieve.
I will use the same method as it was built in there.
And for this one I would like to use the book serializer.
So the default one, the serializer class for an entire view set is the mini one.
Which contains only ID and title.
And as you can see here, so we have that mini one.
But if we will use the retrieve, which is one specific record from database, we will use the book
serializer, which is the full serializer for all the data.
So let's test it.
Now we go to the postman and I will send it again and see here.
It's just a mini serializer shown, but if I will go book ID one, which is hobbit and then I will send
it.
You can see I have all the data available for this.
So if you would like to optimize your database queries.
And for example, we have just a page with a title on it.
What you can do is you can have mini serializer for this because that will what you will need on your
page.
But if someone will click on this ID, you will take this ID from here and you will pass it to your
URL like that, and then you will load it and you will have all the data available for this book.
In that way, you don't need to have all the data for all books in your system.
You will just ask for the data you will need for a certain part of your application and that's very
optimized.
You don't overload it with the data.
You will.
You might never use it and that's how you can create it.
So doing that again in the serializers you can actually have more than one serializer for the same class.
You might decide which one will have how many fields here and.
This is the way we can actually decide it.
For each method that are available in the model view that we can override it and we can decide it.
What serializer will be used for that certain one and that will be the default one.
Okay.
And that's about it for our Django in the introduction.
I hope at this point you kind of understand how it all work and we are kind of ready to start building
our API and that will be our first project.
So we'll go again from the beginning and we'll create our mini application.
That will be our backend API.