Okay, let's set up our views and serializers to expose our models to our API so I can close the admin
now and we have our models here.
And also I will close this URLs here and also I can do with the settings here.
So we have models we need to create a new file and we'll call it the realizers.
PY.
This is the same way as we've done before.
So first I will import.
Uh, sorry.
From rest framework.
Import serializers.
We'll import serializer and we'll create for each our model a new serializer.
So class movie serializer.
And we'll do Serializers.
Model Serializer.
We've done that before, and then we do class meta.
And inside here we can decide what will be in our serializer.
So first the model.
It's going to be our movie model.
We need to import that.
So from current directory models, import movie and then we can decide on the fields.
So the fields will be actually, that should be equal.
Fine.
Sorry.
My mistake.
So the fields should be.
We have available ID then.
We have.
Title.
And then we have description.
And that's the same fields as are we have title description here and ID is added automatically for us
by Django.
So we have that serializer here.
And then what we can do to speed up, I can just duplicate it.
And then another serializer will be rating.
So we need to also change this model to rating.
And this will also import rating here for rating.
What we have is we have ID stars.
We have user and then we have movie.
And we have our serializers set up.
What we also need to do is we need to go to the views and inside the views we'll create a views for
each of the serializer so as before class.
Movie.
Few said like that and then we'll do.
We need to import that.
So from rest framework import.
View set and this view sets we can use here.
Model view set like that.
So what needs to include their query set?
And that's going to be our movie.
Objects and then we'll do all like that.
We need to import our movie.
So from.
Models import movie also will also import rating from here because we'll use it in a second.
So we have our query set and then we'll do serializer class.
What serializer for this movie view set.
We have Serializers here and that's our movie Serializer.
So I can pass it here.
Actually, the serializer class, it's a tuple, so I can do it like that.
And then also I need to import it.
So from Serializers import Movie Serializer and the Serializer class and the query set is set up for
our movies, then I can duplicate this Ctrl D and then I will do rating view set.
And then what I have to do is I have already imported that.
So in this case we'll select all the ratings and also we'll use the ratings serializer.
And this this one needs to be also imported from serializers like that.
So basically that's a typical setup as we have it here, and we've done that before.
So there is not a new I've done it quite fast, but we know how to do it from the previous section when
we talked about Django.
So what we set up so far, we have our models with a different fields.
Then we create new file serializers and for each model we've create different serializer.
We tell the serializer which model we are using and what fields we would like to use it.
So we in that case, we have to also, in the view set in the views we've created, view set for each
model.
So it's a lot of typing here, but basically we are creating view sets using the built in Django rest
framework view set, the model view set here and we set up the query set which is what?
Set of records from database we would like to use.
And in our case, we would like to use all movies.
And then we tell also what serializer we will be using for the movie set.
We're using Movie Serializer, and for the rating view set, we said we use rating serializer.
So all all of that is already set up.
What we can do also is we need to go to URLs here and register our view sets in our router.
So router.
Register and then we can register.
This view set as we see here, I can copy that and register that on the router.
So the first argument will be movies, which is just a string that will be inside our path.
And the second argument is the view set itself.
We need to import that.
So from.
Views import.
Movie view set.
And we have that here.
Also I will register another.
View that which is rating view Z.
So coming back here, I import view set, use it here and that will be ratings like that.
So I save it now and then I can run it.
And let's see what I can do in our I will copy that URL and let's start to test our application.
So I will do like that and then I can go API and then movies.
That's what I registered in our database and we have some problems now.
Page not found API movie, so let's come back here.
So let's try to debug it.
We'll go to the Chrome and let's go to our URLs like that and then we have admin and we have our API.
So let's go to API.
Like that.
And then we have our movies and then we have our ratings.
So everything looks like it's working here.
So I will click on movies and then topple Object is not callable, so we can see our error there.
And which one is that?
So basically that's a problem when we have.
A tuple that has been registered.
Let me take a look.
And Dave, you said and actually I made a mistake because the serializer class can't accept more than
one.
So in our case, it has to be just one serializer like that.
And then I will save it here.
So that's gone.
And then if we refresh it now, you can see I have empty results of movies just to make sure I will
go.
The same with the ratings.
Uh, ratings.
I misspell it.
So ratings like that.
And I have the same information here.
So what I can do is I can come back to the postman and send it again.
Page not found.
Not available.
And the reason for this is so I will copy that.
Let's try this one.
Ratings.
And you can see empty red results there and movies.
An empty vessel there.
I think I had misspelled it.
So that was movie instead of movies.
So our URLs are plural.
So we have movies and ratings.
So we have our application kind of running.
So we have our movies API working and also the ratings.
So at the moment we have nothing in our database.
So in the next video we'll try to create some records in our database.
But for doing that, we are not going to use Django, we are not going to use this interface and that
is available here as we can use it.
But we'll try to do everything in our API in The Postman and that will kind of mimic what we will also
need to recreate later on in our front end application.