Okay.
Once we have added our rest framework tool to our Django, let's use it now.
So we'll close these settings because we won't need it anymore.
And what I will do is I will open the demo and I will create a new file and that file I will name it.
Serializers Serializers.
PY So basically here we will store the serializers here and I will explain you later on what the serializer
is.
I think it will be easier, if you will, when to understand it when you see it.
So first thing we need to do from rest framework that's available for us because we just installed it.
Import serializers.
So we have that imported and then we can create a new serializer here.
And there is a few way that we can actually create a serializer.
But I will do a modal serializer and that will ease for in my opinion, that's the easiest way and more
efficient way to work.
Then everything will be based on models.
So you can nicely sync in views, models and serializers.
So we'll create our class and then we name it.
So what do we want to do?
First we want to display the book.
So Book Serializer, and then inside we can include some built in Serializers modal serializer like
that class.
Meta model.
And then we'll pick what model we would like to use.
Obviously for us it will be book, but I also will need to import.
So we are in the same folder.
So from models import book, so dot models import book and now the book is available.
And then here we can decide.
What fields will include.
So let's start with title like that.
So we've created our serializer and what we are doing here is we name our serializer, we pass a built
in Django methods and then we are telling, okay, we will use our model book and then we would like
to display title fields.
That's it for our serializer.
At the moment we don't use it anywhere.
So let's go back to the views.
And in the similar way we've used before, we can create to here another class.
So we'll do class and we'll do a view set.
So I will import view set from Django Rest framework.
So from rest framework import view sets like that.
So my class will be will be we will use similar conventions we use with serializer.
So we'll do book view set like that and then we can pass view sets and then we can have model modal
view set.
So basically we are creating a built in view for our books that will use all power of Django and it
will create the option for the Http method for us.
And I will show you that in a second.
So serializer class and this is place where we actually tell which serializer we would like to use.
So we have one serializer, and I will use this serializer here, but we don't have it available yet
in this file.
So what we need to do is we need to import so from dot serializer dot because we are in the same folder
import book serializer one this is here, then we can use it.
So for now, just to omit some confusions, I will remove all of what we had before.
I will need to also.
Correct the URLs in a second, but this is basically what we have seen so far.
So we have a imported view set and then we have our serializer from another file.
And then we are telling that setting up a new view set and actually we have like that view set and then
a serializer class.
It's a book serializer.
Also here we need to decide.
What is the query set for this view?
The query set is something that we've done before and that will be our book objects.
And then all and this is we are telling if we ever use this view set, what query set we would like
to use and basically we will do use all books.
Also, this big book needs to be imported.
So from.
Models where we have our book import book.
Now we have we don't have any errors and then we have our view set here.
So I will save it.
Now we have Serializer and that serializer will use our book model and display title.
And then we have a view set.
The way we had it before.
It was a simple class or a function.
This one we this time we use model view set and I will show you how powerful that is in a second.
So we have this and then we need to go to the URLs.
So we need to instead of having this function passed to the our URL patterns, we'll use a router.
So let's import from rest framework import.
Routers and then we can create a new router here.
So router, we can name it as we like.
And then routers, which is that what we have imported here?
And then we will use default router.
Like that.
So we have a new variable, a router and using rest framework routers.
So here we can do router and then register.
And then what we can do is we can register a URL.
In our case it will be books.
And then we can pass a view set.
So we have a book view set here and we will pass to our router like that.
We also need to import it.
So from.
Dot views, import view said like that.
And the last thing what we need to do is we need to include it in our path.
So I will remove this so we'll have an empty path here and then we can do include.
So include.
This is gone already, so we need to import that as well.
We don't need this anymore.
And then we will include here our router URLs.
Like that.
So basically what's happening here is we have our path.
Remember, this path is already inside our demo, so it will be URL slash demo and whatever.
Here we'll have it.
It will be accessible through from the router.
So at the moment we have register only one model, which is books.
That books will go here and then it will use all what's available on the view set.
So let's run our server now.
A this supposed to be as URLs.
I will run the server again.
And then we have our server running.
So let's come back here.
To our.
Demo.
And look at that.
Now we have another view.
And that view comes free with the Django rest framework, as you can see here.
And that's kind of UI for managing our rest.
So what we see here, we have some methods here.
So basically if I will use this URL in some kind of API browser and I will show you later on how we
can do that, then I will see a Json.
So what we can do is I can click on this link and you can see here I'm seeing a list of our books.
This is what we added in our database.
And this here, this part, it's a Json, it's an array of objects and each object has a title and a
value.
So this is what we will use actually in our front end application.
And the reason why you see title here and nothing else is because we added in the serializers here.
So if I will do another field description and let's say price.
I will save it.
Now coming back here, refresh and you can see for each object here we have title description and price
available for us.
So in the serializers we decide what should be included in our object.
So next time we will use a front end application.
I would like to have a list of books from our backend.
What we need to do is we need to use that URL.
So our domain then demo, which is our application and then books.
Books will register that as a view set in our router and then it will give us an array of objects like
that and then we can do whatever we will we want we need to do in our front end application.
So this is how it works and how the Django Rest framework works.
This is just a handy another UI that we can use.
And also you have another section for posting the data.
And if I can, uh, go for example here books one and then slash, you can see here also I can update
the data here and I will show you later on how we can actually use that different methods.
So what's available for us is get put, patch, delete head options and so on.
So this is typical methods for a rest API.