Hello everyone and welcome back.
In this lesson we're going to learn how to use them more than you realize that class simulator to jingles
model form classes Model C realize it allows us to speed up the creation of serialize US based on models
we defined providing us all the code that is needed for the most common development use cases.
So let's get started.
OK.
So here we are in Visual Studio code and as you can see I've opened the serialized this dot by five.
In this lesson as we said we're going to talk about the Model C realized that a class that in fact is
very similar to the serialize that a class you can think about the more than a C realized that a class
is the name suggests as a serialized that a class that is targeted towards a creation of satellites
US based on models that we've created in the models not by file.
So how does the model serialize that class and pass in the development process.
Well for instance it will automatically generate for us a set of fields based on the model that we want
to create.
The satellites therefore which means that there is no need for us to specify all the fields as we DDA
with the normal let's say sterilizer classes.
It will automatically generate validators for the serialized such as unique together validators and
it includes seeing both the implementation of the create and update methods.
So basically it allows us to get a lot of code for free.
So if we go and have a look at the serialized class itself and we see that it marries from base sterilizer
it has a couple of methods.
But basically when you really go and have a look at the code you understand that you basically have
to write most of the code in fact that you need to build this analyze it depending on your use case.
Let's now have a look at the code of the model serialize that class instead.
So I'm just going to search for model so you can either end it it is.
So you see a model serialize that is just a regular sterilizer except that a set of default fields that
are automatically populated.
Same thing for a set of default validators default to create and update implementations are provided
and we can if I look at the code to understand how it is the default create method a default update
method and so on forget fields get field names et cetera et cetera.
So let's go back to the realized there not by file and let's make our own version of the article sterilizer
class using this time that model C realized that class.
Let's see how we can implement it.
So I'm just going to comment this out like so and then right on top of the file I'm going to define
class the article serialize that which this time is going to inherit from serialize this dot model serialize
it.
So first thing we need to do is to define that class matter and within a matter class we're going to
specify model which is going to correspond clearly to the model we are building our satellites therefore
and how we need to tell Django this framework which fields of the model we want to serialize and there
are three ways we can do so.
First of all we can say that we want to serialize all the fields of our model.
Therefore we might want to define fields equals all like so.
So in this way we are basically telling Django we want all the fields of our model.
We can however specify just a couple of them.
So we could do something like fields that we can use it to pull and so let's say title and description
and maybe the body of that article and it is where we are basically telling Django it's free mode that
we want to choose a couple of fields.
And then the third way is to just define which fields we want to exclude.
So we can do something like exclude equals that tuple or at least then for example we might want to
exclude the I.D. the primary key.
So let's restart our development server and let's go ahead.
Let's go back to Chrome.
Let's go ahead and check our new serialized behaves.
So let's go back to the list and perfect.
Did you go through a list of articles but this time as you can see DHEA field is hidden from the user
it's easy to grasp how powerful the modal serialized that a class a release.
Just have a look at how much code we've saved and it gets even more powerful and useful when you take
into consideration the fact that you can actually define some extra fields for your sterilizer.
So let's say we want a field which returns the time since publication for each and every article.
It's really easy.
Let's define the name of a field so time since publication.
And we need to initialize it as serialize US DOT serialize the method field.
Now we need to define a method which by default is going to be called get and the score plus the name
of the serialized automated field.
So right here I'm going to define get their score time since publication gets time since publication
is going to be the method which is going to return.
In fact the value for our time since publication field so he's going to accept sell for an object as
parameters.
I'm also going to need to import two functions.
So right here on the top from Date Time report days time Dan from Django not utilize dot time since
import time since.
So let's now implement our method.
So a publication date publication date equals object which is the instance that publication date then
we need to date time at a time of a request.
So now equals days time now.
Now we can get our time Delta.
So time Delta equals time since and we're going to pass publication date and now pretty easy.
We can now return time delta so that its value can be used as value for time since publication.
Let's now restart the development server.
Perfect.
Let's go back to Chrome.
Let's refresh let's make another get request.
Perfect.
And now we got as you can see an extra field time since publication which in fact does exactly you see
what the name suggests.
Let's make a quick inspection of the article serialize there from Jangles shell so we can see how it
relates to what we've seen in the previous lecture when analyzing the article serialize that made with
the serialized hourglass.
So I'm going to stop the development server and go into spawn a new terminal.
So we are here Python managed that by Shell.
I'm going to expand the terminal like so.
So from news that API that serialized this import article analyzers or better article sterilizer let's
just make a new instance see the equals
article sterilizer and let's have a look at its representation.
So print wrapper of our civilization and as you can see it's basically the same as what we've seen in
the previous lecture except that now we've got the extra fields
and let's variety the middle classes there.
Know once again perfect so that we can check the basic implementation of create and update.
Let's go back to.
And I'm not going to create in New article.
Maybe this one like song went to David Fields and times since publication.
And I'm actually going to change the publication date so let's say we want to publish it in the future.
Let's say we're scheduling the publication and yet have got saved and Laramie so we can use for it the
title like so some description and maybe location Mars.
So let's now create our new article and edit is awesome.
It says as you can see publication date 0 minutes which is something that we might expect considering
that the publication date is in the future.
But most importantly everything worked as expected.
Let's now see the update method behaves.
So I'm going to copy the Jason.
Going to delete those two fields
so maybe I'm going to change the author's name so maybe John Peter.
Let's see what happens.
Puta perfect is you can see the article author is now John teeter.
Considering that the model C realized that a class is just like a very powerful allies at a class we
can of course define all the same validation criteria that we've defined right here in the previous
version.
The exact same way so I'm going to comment the code I did a link to the documentation to make it easy
for you to check it if you want to.
So let's restart the development server.
Perfect.
Maybe we can actually change the length to maybe 30 characters here as well.
Let's go back to Chrome and let's try to trigger somebody nation error.
So going to create a new article
so
I'm going to use the same description or maybe same name as description like so post and edit is for
and read by the request.
Let us know try to trigger the second validation error by creating an article or rather updating the
article to have a title which is not 30 characters long.
So
put it for android by the request.
The title is to be at least 60 characters long and that's because to be honest in fact you need to always
update the error message to 30 like so well let's go back here let's refresh.
Perfect so school back here number 7.
So
okay.
So that was it for this lecture.
As you can see the model is the analyzer class provides us a very convenient way to create C Eliza's
based on models we've defined and as a matter of fact.
Because of this convenience this is the class that we're going to use throughout the rest of the course
in the next lecture.
We're going to talk about another very important topic which is nested in relationships which means
he realizes.