Hello everyone and welcome to this new lecture about Syrian ISIS.
Syria ISIS allow complex state that such as squarely sets and model instances to be converted to native
python on data types that can then be easier rounded into useful formats like Jason.
This process is known as serialization of data Syria lasers are a very important component of Django
this framework that we can easily use by employing this utilize the end model utilize their classes
serialize their source or provide the death serialization allowing past data to be converted back into
complex types after first validating the incoming data.
In this lesson we're going to learn how to use this serialize their class to sterilize and the serialized
data from a radical model that we've defined in a previous video.
We're also going to briefly talk about passives and renderings.
Let's get started.
Okay so we are invisibles to your code and the first thing that we want to do is to create an API folder
within our news application and then within this API for there we're going to create all the files that
are going to be part of the API itself.
This is not strictly needed but it surely is best practice and it helps us to keep the code much more
that than it would otherwise be.
So new folder I'm going to call it API.
Now within DARPA for that the first file that I want to create is that she realizes that pi file which
is the name clearly suggests is the file where we are going to define our serialized their classes considering
that we are going to use a class that is provided by Django rest framework.
We need to import to the module first.
So from this framework import analyses and we can also take a look at the modules code so we can just
right click go to definition and we've seen the module as you can see we can find some pretty useful
comments.
And the first line reminds us that as we've said Django this framework uses part of Django programming
conventions making it really easy to understand as we can read and is read soon going to see realized
that a sand Model C rises are similar to forums and model forums in Django and like forms they are not
constrained to dealing with age the email output and form encoded input.
I definitely suggest you take some time to take a look at the code.
Did you your importing from every module normally did you're importing for the first time.
Because especially if it is part of some well maintained an well known framework or package like Django
or Django framework it's definitely going to be very well commented and it's going to give you a ton
of useful tips.
So let's get started by creating R C analyzer class and considering that we want to create these serialized
that class for our articles model.
We can just call it article sterilizer which is going to extend C realizes that serialize it so we've
read that C that is the same forum setup very similar and in fact so much so that if we take a look
at the general frameworks documentation about C realizes we've got a whole page dedicated to serialize
their fields that are in fact very similar to what it was we would use we've Jane goes forms we have
for example boolean fields we've got string fields so Chatfield for example he made field the lack field
and just taking a first look at the menu here on the left we see that we also have numeric fields so
integer flowed data and time fields choice Fields file upload and so on.
So I definitely suggest you go and have a look at Django those frameworks documentation and maybe even
save it among your bookmarks because especially the API guide becomes really handy.
A lot of times.
So for now let's go back to these what is your code and let's define the fields in this case from our
model.
Do we want to use we V not serialize it.
So first of all the idea is the primary key of a given instance.
So serialize this dot integer field and we have to pass the parameter read only peoples through because
ninety nine point nine percent of the times we do not want to modify.
JD Did yes to be a read only field so we've got the author which is going to be realized that dot char
field and saving for the title the description.
And even the body even though within our model stood by fire we specified body s text field that doesn't
make a difference.
E.R industrialize their representation then we've got location which is going to be Chatfield as well.
Publication date which is going to be this time date field then we've got active boolean flag created
at and updated at so active it's going to be boolean field White created at.
Going to be eyes that start date time field we want to pass the read only parameter because this field
is managed by Django automatically and you think you're going to do for.
Updated at notice how no matter the kind of field that we're using would say I'm going to get a representation
adjacent representation of the field thanks to the serialized field and see realize that a class there
is no need to spend time thinking about how we might want to represent a certain field from our model.
Because Django This framework provides us everything that we need.
We working at a higher abstraction level where most of the troubles that we've come across in the previews
section have been solved.
The ready.
And we can now focus only on what we want to do and to be fair.
Now that we're actually working with a model there is even an easier way that we can use to actually
see realize the data of our model.
And that implies using debt Model C realize that a class that we're going to learn to use later on but
for now it is important to work with debt serialize their classes in order to understand how the process
really works.
We now need to define two methods create method which accepts clearly self and validated data we can
use the past instruction for now and then the update method which accepts self and instance and then
as for create validated data as the name suggests they create and update.
Methods are the ones that will provide the code to actually create and update instances for our article
model and in fact considering that we're going to work with this model we actually need to import it.
So from news that models import article and I'm also going to make a short note if your code gives you
an error here in import a statement using the news dot models which is the up name dot up file.
That's because you haven't used their projected folder as in fact the main folder here on the tree.
I don't text editor doesn't give this kind of errors but B's words to your code does.
So let's start by creating by writing the code for our create method.
So we want to return article dot objects dot create and we're going to pass out validated data and let's
also add a print just for now on validated data.
We're going to take a look at the result of this print later on and this will give us an idea of what
we really are working with.
Now let's write the code for our update method.
So we're going to update a specific instance that is passed.
So let's start for example by the author field possibility that of course is we said I.D. read only
so instead not auteur equals validated data.
Dots get.
We get author for instance not offer.
Basically if there is no data that is being passed and just use the one did you already have.
And then we going to do the same for all the added fields.
So instance that title validated data and not get title or instance the title then instance that description
yeah.
And we're going to actually close this one for now.
So Dan
right below and it's rather effective to copy.
Three more times two and three.
So we got Buddy here and we've got location okay sure.
Publication date and the active flac
and in fact I can also indent the second parameter like so one to
and I can open up the tweet.
Once again.
So now that we're assigning all the new values let's say to every instance we actually need to save
it.
We can just call the safe method of the ESM itself and we need to return it.
So our articles utilize their classes almost daily.
I see that there is multiple years not publication data but publication date.
Same thing here.
And now that our article serialized that a class is finally ready we can get to the details of our their
serialization and the death serialization processes really work and we can do so from within.
Jane goes interactive share.
So I'm going to create a new terminal terminal new terminal.
I'm going to go in expanded mode so Python managed that by Shell.
And so first of all we need to import Beaufort article model and article serialize their classes.
So from news dot models import article and from news that API that serialize its import article synergize
said.
Also considering that in the previous video we've actually created an instance of article we can now
call it.
So article instance equals article that objects that first and if we take a look at it now article does
and we get the string representation according to what we've defined in Dan their string which means
that we have the article author's name and then of course the article name itself.
The article title and we can now serialize the data of these specific article instance thanks to our
article realized that a class.
So we do like so sterilizer equals article utilize it and we want to pass two articles serialized either
article instance so article states like so and if you take a look at it see Eliza like so we see that
we've got our articles utilize edit to which we best our article.
Instance we see that we've basically converted all the fields that we've defined within that article
serialize their class into python on native data type.
We can take a look at them like sort utilize the DOT data you see it in how we got a dictionary with
all the specific values of all the fields of our modeling instance.
And so keep in mind that data are this data that we're going to see a lot during the course in the form
for example of a request dot data represents the data that we're using at the serialize that we have.
So in this specific case it represents clearly the data of our article instance I repeat we're going
to see data a lot during the course that what it represents.
It represents data to finalize the sterilization process we will enter the data into Jason and we do
like so.
So first of all we need to import adjacent renderer.
So from rest framework that renders import Jason render.
And now we can do like so Jason equals Jason Rand did it that render and to the trend that we pass serialized
in that data we can now call Jason and we got our data rendered as you can see it's very similar to
the one that we've actually seen here using realized that the data directly you can spot the difference
for example in the active flag you see here we go through with the capital T as we do in Python Yeah
we got active who if true that does not start with a capital T so let's not talk about this serialization
process first of all we need to pass a stream into Python native data types so we need to make to import
import I O which is just a Python module there to offer as an interface for handling data streaming.
So now that we've rendered our data we actually need to pass it.
So we need to import the parser.
So from this framework important Jason pass it.
And of course we get in there because it's not from the rest framework directly but it's from the rest
framework that parsers import.
Jason positive.
So enter and so first of all we need to get our stream with the like so stream equals are your dot bytes
are you Jason.
And now that we've got our stream we can get our data back.
Thanks to the bar set so data equals Jason power said no to pass the tweets we pass stream.
So in doing so we're getting that data back and we can now use the articles serialize their class once
again to restore their native data types into a dictionary of validated data like so that I zero equals
article sterilizer.
We need to pass data equals data.
We need to check if the data is valid.
Otherwise we will get an error.
So serialize that dot is valid through perfect because of course our data is being processed properly.
We can now actually ever look at it sort of see tonight that dot validated data.
So our dictionary of validated data we can now save the city serialize there so serialize it that's
safe.
So now that we've called Save you see did we get back a new instance of article which is basically a
clone of the one that we have previously created but that in fact is a new instance.
So if we now do article not objects not all you see we've got quite he said we have two distinct instances.
So now that you've talked in great detail about the sterilization and desalinization process let's talk
briefly about process and Iran that is.
So we've used the both of them in these examples and is therefore better to give a bit more of information.
So a part of it is a class that allows the rest api to accept and comprehend different kind of requests
and clearly Jenga this framework provides different kind of parsers one of which is Jason parser that
we've used but we also have form a parser or multipart parser for example that can be used to send and
receive images or other kinds of files.
The really cool thing is that generates framework will automatically decide which kind of parser is
optimal for a specific request based on the value this is associated with the content type header that
as you might remember from the requests lesson is responsible for specifying the kind of content the
type of content in fact that is used in the requests a renderer class on the other end allows us to
provide different kinds of response or any way to personalize them.
We have Jason renderer clearly but we also have template HDMI renderer or arousal API renderer and so
on.
So that was it for this lecture.
I hope you enjoyed it.
I remember you did you can download all the slides used in the course and that by the end of each lecture
in the slides you're going to find a lot of reference links that will allow you to sensitively deepen
your understanding of all the concept that we've been discussing in a specific lecture if you feel like
so.
So that was it for this video.
See you in the next lecture.