Now, as we kick things off of working with this jingle rest framework, the first thing that we're
going to talk about is serializer is now serializer are a way to sort of be a middleman between models
that we have in our project.
Right.
In this case, posts and vote and also the API, they sort of translate, you know, if this is a Django
model, how do we turn that into a Jason object and how do we do it go?
Vice versa.
Right.
If someone passes us some, Jason, that they want saved as a Jenko model, how do we do that communication?
It's using a serializer.
So what we're gonna do is move over to our post app.
This folder.
And let's go ahead and create a new file here.
So I'm going to right.
Click new file and I'm going to call this serializer as serial Kaisers dot pi.
OK.
So we'll go ahead and save this.
This isn't doesn't have to technically be this name, but this is what the Django Rest framework suggests
for the name of this file.
So it's good to sort of follow their styling as you get to building out your project.
And the first thing that we need to do is import some other code.
So we're going to say from Rest., underscore framework import.
And we want cereal.
Isaiah's.
OK, so now that we have that, we have to create a serializer for each model that we're working with.
So let's go ahead and start first with the post model.
So since we're going to be working with this class, we have to import it.
So let's go ahead and import that up top here.
We're going to say from Dopp models.
Import, we want capital posts and we have the DOT models because we're staying in the same folder here,
the same app.
OK.
So with that in place, we're gonna create a class, which is the way that we make a serializer.
We're going to say class and we're gonna give a name that describes the serializer.
It's a it's for our post objects.
So we're gonna call it post serializer.
Now, it doesn't have to be this name, but I think it's kind of, you know, describes what's going
on here inside of the parentheses.
We're gonna have to say serializer is dot and we want capital model.
Serializer, now, there's all different kinds of serializer is out there, if you go ahead and check
the Web site for the project, you can see all the different ones out there.
But we're going to use the model serializer, which basically makes it really easy to say to take some
model that we have and be able to translate it into Jason or whatever else we need in our API.
So with this, what do we add into the class?
Well, the first thing that we have to do is say, what fields do we want associated with the API?
And if we go ahead and look back at our model, right.
Like a post has a title, it has a your real poster when it was created.
These are probably all things that we want in there, but there's going to be some additional pieces
as well.
We want to know what the vote total is.
Now, even though that isn't part of the model here, we'll come back and add that later.
But it is something to think about.
So the serializer is basically a way that you can connect your fields and have some additional properties
that you want showing up inside of your API.
But for the time being, let's take the basic fields.
I'm going to copy these four lines, bring them into this project just so that I you have a reference
of what I'm looking for.
So I'm going to type out class meta.
Colon and then in here have to specify what model I'm basing the serializer on, so I'm going to say
model is equal to capital post, which we just imported up there.
Post serializer.
There we go.
And then I have to say what fields I want included with this.
So this is just looking for an array of strings that's gonna be the name of the fields.
We're gonna start first with I.D..
Now, IDEA isn't explicitly listed as a field, but it's included with every Django model.
So we have that there.
And then I'm just gonna start using what we have here.
So I'm going to copy title here.
Add that in.
Then going to add you URL.
K poster.
Go ahead and put that there.
And also created.
Great.
So this is essentially saying, you know, when someone wants to get some sort of post from our database,
it's going to come out as a model, go through the serializer and eventually turn into Jason.
And this is literally all we need to get started here.
So we'll go ahead and save this.
All right.
So we've got our serializer here.
Let's now move to creating our very first API.
You are l oh, this is so exciting.
So I'm gonna go ahead and move over here to my you URL's and we're gonna be adding a new path so you
can decide how you where you want your API to go.
I think a really nice way to do this with most Web sites is to started out with API and then slash whatever
sort of resource it is that you're going for.
So, for example, we're trying to get the posts shown to the people.
So I'm going to say API slash posts, OK?
And with that, I'm also going to now say where this is going to, which means we're gonna have to import
the view from our posts.
We're going to say from posts import.
Views.
So now I can say views, Dot.
And then I need to get the name of the function or class that we're gonna be using.
Now, with the Jango rest framework, you can use either functions or classes.
But I think the framework really shines when we're using classes.
So now's the point.
We're gonna go over to our views and create our very first view.
Now, there's lots of things that we want to do with this API, but I'm gonna start first with just
the ability to list out all the different posts that are saved inside of the database.
So I'm going to make a new class here.
And I want this to be a post list.
It's going to list out all the different post objects so I can do my parentheses here and a colon and
inside of the parentheses.
We have to pass in some code and we've got to import this from the framework, so to say, from rest.
Underscore framework.
And I want to import.
All lower case generics.
OK.
So with that in place inside of the postals parentheses, we're going to say generics dot.
And then we want capital list, capital API, capital view.
All right.
Now, what this is doing is it saying, OK, we're going to make a class based view.
And it's going to be listing out information that's going to help us do that very quickly.
Here's what we've got to do.
We first got to say, you know, what is it that we're hoping to display in the end?
What do we what are we pulling from the database?
And that's called the query set.
So I'm gonna type here all lowercase query set.
I say this is equal to capital post dot objects, dot all the parentheses.
Now, in order to use post, we've got to import that.
So again, I'm going to say from DOT models, I want to import capital post.
OK, so that's the query set.
That's what we're looking to pull from the database.
And then we have to specify what serializer we're going to use for this particular view.
And remember, we just created one the post serializer.
So I'm gonna go ahead and copy this name.
So I have it.
Exactly.
I'm going to come back and say that the serial serializer underscore class is equal to the post serializer,
which again, we've got to import this.
So let's say from DOT Serializer is import the post serializer.
And with this in place, this is all we need for our very first API call.
Yes, this is it.
OK.
So I'm going to save the Vieuxtemps pie.
I'm going to copy the name of our class based view here post list.
I'm going to copy this.
Come back to your URLs and say if someone goes to API slash posts, send them to views.
Dot post list dot as.
Underscore.
View with the parentheses like that.
And this is all we need to get started.
OK.
So I'm gonna go ahead and save this.
Let's go ahead and move to our terminal and run our project song to say Python Managed Pie runs server.
OK, so this is now up and running.
And you can see it's here at Port eight thousand, so I'm going to copy that in Safari and I'm going
to plug that in.
And here you can see we've got nothing set up for the home page.
That's fine.
But look what happens if we go to slash API slash posts.
Look at this.
We get something.
It kind of says, OK, you know, Jenko rest framework.
You're thinking, OK, this is kind of cool, but there's there's no information inside of here.
Now, the reason for this is because we don't have anything saved into our database and we've got to
do something about that.
So in the next video, we're going to talk about how we can add some information into the database here.