Hello everyone and welcome back.
In this lesson we're going to learn how to define nested relationships in our serialized classes as
an example.
We're going to extend our news API project by creating a new journalist model that we're going to bind
to our article more than we've a foreign key field instead of a charter field that we're currently using.
We're going to learn how to define for each side of the relationship and for each instance a list of
primary keys or strings of related objects.
All the details of a specific instance or an automatically generated a bit link to the corresponding
end point.
We're also going to talk about went to actually make those relationships explicit by providing all the
details of the instances involved.
I went to opt for other design choices based on the development context instead.
Let's get started.
Okay.
So here we are in Visual Studio code and as you can see I've currently opened the Model S dot by file
because we need to create the journalist model first so that we can bind it to the article model in
order to make some examples of nested relationships.
This is going to be pretty easy.
So class journalism models that model and this model is going to have three fields first name which
is going to be our field so models not char field with max length 16 then it's going to have a last
name field and a biography field models dot text field with black true so that we can make this field
optional that we need to define it down the string method.
So self return we can use a formatted string literal so self got first name.
So Dan we can add the last name as well okay.
We now need to create the relationship between the two models.
So I'm going to comment out the author field like so and yeah I'm going to define author models dot
for a key and I'm going to point to journalist
with on delete model dot skate and we can also specify a related name parameters so the related name
equals articles and I can just as well delete these comments.
So let's create our migrations new terminal like so.
So Python manage that by
make migrations so create model journalist alter field offer on article before we give them migrate
command.
There is one thing that we need to take into consideration considering that we've just after to the
field author on article which was previously our field and now with key applying their migrations on
our current database would result in errors trying to access the previously created article instances
we could have in fact specify the default parameter for our field or we could have deleted the previously
created article the instances before altering the model.
But another thing that we can just as well do is to delete our database applying all the new migrations
afterwards or a newly created one which is what we're going to do.
This is clearly something that you do not want to do in production of course but if we can still afford
it is perfectly fine.
So remove database that sequel line 3 and now Python managed that by migrate going to create new Subarus
the python manage that by create super user admin
we can now register the new modelling to add me note by file journalist
to so let's run a development server now
and let's open up google chrome.
So going to go to slash admin
I'm going to create an instance of a journalist first.
So John Peter Time Traveller save let's create an article for instance.
So author John teeter title.
This is the first article we are publishing description first article of the new database.
Here we can just write some content like so location you can say Rome publication date today flagged
as active save and add another.
So either this is our second article and maybe a couple of characters more so maybe drastic article
random description random content location can say Milan publication date today save.
So let's not go and have a look at our API is actually representing the relationship between the generalist
model and the article model.
So let's go to slash API is Lesh articles and it is our list of articles in case you're wondering time
since publication says 16 hours because as you can see at the moment is almost 16 PM.
So let's leave that aside for a moment and let's focus on the author field instead.
As you can see by default the relationship is expressed using the primary key of the related object.
So we go to Jason with all the details of an article ESD and we go a specific field.
The author field which is is the primary key of the related object as its value.
Let's see how we can spice things up a little bit.
For example by showing the name of the author instead of its primary key.
So let's go back to visual studio code in the serialized is done by file and here in the article sterilizer
we can not specify the name of the field.
So author
equals realizes dot string related field.
Let's restart our development server and let's go back to Chrome.
Right away.
Let's make another get request.
And as we can see the relationship is now expressed using a string which represents the name of the
offer to be precise the relationship is expressed using what we've defined in the down the string method.
Let's now go back to these to your code and let's see how we can actually use another sterilizer to
represent all the data of the related object in our sterilizer.
Basically we're going to use another serialize it to sterilize the data of the deleted object.
We need to create our journalists utilize their first.
So Plus journalist
sterilizer realizes that model C that I said So class Meta model equals journalist which we actually
need to import like so that we can just say that we want to use all the fields of the model like so
now we can't take the name of a serialized.
I can just comment is filled out so I can define author as journalists sterilizer and we actually need
to move the class right here on top like so.
So let's restart that development server and let's go back to Chrome and let's make right away now they'll
get the request.
Also.
Perfect.
We now get another Jason with all the details of the Hoffer for each single article instance in this
could prove to be in fact really helpful because at the moment we do not need to make any other request
to get all the details of the author.
However there are some drawbacks which we must take into account when creating our API.
For example let's try to create a new article the instance going to delete all the unnecessary fields.
And the hot Fairfield in the time since publication.
So this is another article.
Yet another awesome title.
So let's try and create this new article and you see we get an error age GDP 400 bed request author.
This field is required.
We could go ahead and you know try to work around the limitation by specifying read only through.
So I'm now going to refresh our development server let's try to create the article once again so I'm
going to the proper end point.
Like so post.
And of course we now get another kind of error.
So integrity error not a normal constraint failed news article not author idea because in fact we cannot
create an article without a journalist because journalists are those who write articles.
Therefore we necessarily need them in this particular context.
A better design choice could be to make the relationship explicit from within the journalist serialized.
So let's go back to visual studio code and dear I'm just going to comment this out and I'm going to
move the class right below like so.
Indira I'm going to define articles equals where I'm going to use article sterilizer and I'm going to
pass read only equals through so that we can keep the opportunity of creating new journalistic instances
without having to pass an article least all the time.
Other parameter that I need to pass is many equals true because this you realize there is going to be
used to serialize the whole query set.
We now need to create a new view to actually show a list with all the journalists.
So I'm going to import the model journalist and the sterilizer journalist.
I can just copy to name like so so right here below and I can also comment to can move them.
On the bottom of the page like so.
So I'm going to fix the code one two and I can now create the new view.
I'm going to use the API view class to create this view so class journalist list create the API view
API view.
And yet I can just copy and decode that I've used for the article least create API view.
So this is going to be journalist equalizer here and here.
And this is going to be journalists which also need to best Let's utilize it.
And this is going to be the journalists Modo like that even though the query it is going to be different
because this time I want to have all the journalists.
Now we need to connect our view to it you et end point.
So I'm going to added to our tuple
so I can copy this and find like so and is going to be journalists
journalists at least create TPA view Germany's list and we're now ready to check out and you end point
so I can restart the development server.
Perfect.
Let's go back to Chrome India.
We can now go to slash API slash journalists
awesome as you can see we now get to least with all the journalists in our database even though at the
moment we only have one instance.
But more importantly we see that now we get to list we've all the articles that are related to a specific
journalist and we can also create new instances without any problems.
So let's try to create and you weren't going to delete everything up to the idea like so maybe we can
sense and do something like Johnny Cash and biography so run don't text so let's create a new instance
post.
And it is perfect as you can see we get no problems this time.
And right away we see that we got a list of articles even though the list is empty and we can clearly
have a look at at least and point and edit is a second journalist instance.
Another way to express nested relationships within are serialized this involves the use of the IP or
linked related field as we have briefly mentioned.
This is a field that allows us to get a direct link to the proper endpoint for a specific resource.
We can set up an IP link related field like so let's go back to the serialize is not by file and yet
for example we can just specify particles equals serialize there's not hyper linked related field just
going to comment this out like so and yet do we need to define three parameters.
First of all many equals true then read only it was true and then we need to specify view name which
in fact represents the name of the view basically of the real end point that we want to use to actually
generate automatically these hyperlinks.
So in this case I'm going to use the article detail name which is clearly the name associated to the
article detail API you end point and using this name.
Indeed I believe the related field which results in us getting automatically a URL to the article detail
end point which is going to give us the details of this specific article instance litigating the link
for.
So let's go back to the serialized thought by file.
I'm just going to pass article detail perfect.
There is one last thing that we need to do.
Because the way the hyperlink related field works we need to specify another parameter to pass to our
serialized class and this parameter is context which we're going to use to pass the request to our sterilizer.
So now that everything is done we can restart our development server so let us go back to Chrome let's
make another get request.
And as you can see we now get to least we have all the other points that are associated to the details
of a specific critical instance.
We can now make a get request the Words the specific endpoint and this way we can easily get all the
details of our articles.
So this is how you make nested relationships explicit within serialized sets in Django framework as
you can see there are several ways that we can do so and potentially we could add more for example by
creating another specific serialized or to use let's say in the detail end points another pretty common
way to do things is to just leave that primary key as value associated to the related object and then
make a get request each and every time you need some details about the specific instance you're working
with.
So that was really it for this lesson about the nested relationships in the next video I'm going to
introduce you to the competency assessment project for Django framework level 1.
See you there.