Now, as far as the organization of our project and deciding where it is that we move next, I think
a really great way to take an existing project and add an API to it is look at the existing projects.
You are El's, for example.
If I go to the Urals, that pie here, you can see this is about everything that the Web site can do.
Now, we're not going to do an API for the admen, but we do want auth stuff and we do want all of these
to do things.
And so this can be a really nice way to come here and say, OK, what thing do we want to check off
first?
First.
And I want to start as simple as possible.
Let's first just work on being able to show completed to DOS.
Right.
We want to replace what's happening here.
So step number one is if someone wants to go to our API slash completed to see all the completed to
DOS.
Well, you know, we've got to create a U.
RL for that.
So let's go ahead.
Move over to the Urals dot pie.
That's inside of the API folder.
And the first thing that we've got to do is remember that with the Urals file, you got to have a U.
URL patterns, which is equal to an array or a list in Python.
I'm thinking of other programming languages here and inside of the are old patterns.
This is where you provide all the different paths.
So in order to use path, we've got to import that.
So I'm going to say from Django dot your El's import.
And I want to import path.
Great.
So now I can take this tab over and say, let's do a path for when someone wants to see a completed
to do.
Now, when we're talking about the API here, I think a good way to work with API is, is to have names
for the different models or resources that people are trying to get to.
So, for example, if someone is just trying to list out, you know, the current to DOS, I think a
really good path through that is just to dos like this.
And if they want to see the completed ones, I think kind of a good way to distinguish that is to add
a slash completed.
Now the total path here, if someone wanted to see the completed to DOS, it's going to be, you know,
our site slash API slash to slash completed.
Right.
Because it's first going to come here, say, oh, they have an API.
So I'm going to move them to this file.
And then it'll come here.
Okay, so we've got that part down now.
We need to say, you know, what view are we going to pass this on to?
Which means we've got to import the views here.
So let's say from DOT import views.
Right.
And then I'm going to take our views and we need to create a new class based view here.
So we're gonna go ahead, move over to our views.
And let's make that now.
With this, we're gonna have to import again some of the Gengel rest framework here.
In this particular case, it's the generics.
So up here at the top, we can get rid of this render.
We're not going to be using this.
This is an API.
We don't have to send back each email.
So what we're gonna do is we're gonna save from Rest. underscore framework.
We want to import and this is where we want generics.
Great.
So now that we have that in place, we can create our very first view here.
So this is for the completed to do items.
We're going to be listen out.
So let's just say class.
Let's call this to do.
Completed.
List.
I think that sort of describes what's going on here inside of the parentheses, we want to pass the
generics that we just imported and then we're going to do dot capital list all caps, API capital view.
OK, so now that we've got this here, we've got to talk about, you know, the different pieces.
But before we dive too close up to this, let's go ahead and copy the name that we have here.
This is what we were looking for in the URLs.
That pie paste that in.
And then do a dot as underscore view with the parentheses there.
And then we'll add the trailing comma, save that file.
So at least we have things to the point.
Now, if someone comes to API slash to do slash completed, we're going to then send them to this particular
class inside of our views.
So let's go ahead and move back over there.
So remember, with the Django rest framework, when we are going to be listing out something, we have
to specialize what serializer class we're gonna be working with.
So I'm going to say serial.
Isar underscore class.
And we need to set this equal to something, which means we need to create a serializer.
So let's go ahead and right.
Click on this API folder.
We want a new file.
We're going to call this one.
These serializer is.
Serializer is pi k now.
Inside of our serializer dot pi.
I've got to again import some code from the Jinkx jingle.
Rest frameworks are going to do rest, underscore framework import and then we want serializer is with
an S.
And then we're gonna what we're gonna do is make our first serializer.
We need one to represent D to do model so that to do model does not live inside of our API app, it
lives inside of the to do app.
If we go click on the models, that pie here you can see, oh OK.
You know this is what makes up it to do to dial a title Memmo created date completed important in user.
Awesome.
OK, so let's go ahead and make sure that we import this.
I'm going to go back to my serializer dot pi and I'm going to do a from to do the name of that app DOT
models.
I want to import capital to do just like that.
So now that we have this in place, we can go ahead and create our serializer.
So we're going to say class and let's just call this the straight up to do.
Serial iser.
OK.
Inside of here, we want the serializer as dot model, serial.
Iser.
Let's do the colon there.
And with this, remember, we have to specify what things we're looking for, something to say, Class
Medha.
And the model in this case is the to do that, we just importers are going to say models equal to.
To do loops that want to leave it, to do comment.
So make sure that's got the capital t like what we're looking for.
And then we have to specify what fields we're looking for.
So I'm going to do fields here again.
I think a really nice and easy way to do this is to go over to our model and just copy all this code
here.
Come back and paste it and we can sort of use this as a reference, so we want to always start first
with I.D. and then after that I'm going to copy the title, paste that sucker in.
Go ahead and grab the memo.
A created.
We want the date completed.
So you were just moving down the line here and we want.
Important now in this particular case.
We do not need the user because only a user can access their own to DOS.
That's a big part of this app here, is that, you know, if someone makes it to do that's personal
to them, we should not let other users see what that is.
And so we should at no point need to pass that back via the API because they should be only able to
access information for the API for things that they themselves have created so we can go ahead and delete
all that.
There were not can include the user.
The one thing that we do want to mention here is that the field created in date completed.
This should never be able to be set by the user created just happens when the to do object is created.
Django automatically timestamps it for us.
And then the date completed that is set specially when someone says I have completed this thing.
That's not something that someone should be able to edit later via the API.
So for both of those fields, we want to make sure that we specify that those are read only fields.
So we're just going to type the name of these.
For example, we're going to say created.
Is equal to our serializer dot capital.
Read capital.
Only then capital field.
OK.
And we want almost the exact same thing for the date completed.
So let's just copy and paste this line and then we're gonna take our date completed and paste that in
there.
OK, so now that we've done this, we have a functioning serializer that we can use.
I'm going to go ahead and save this.
Move back to our views.
Man, you'll you'll see we were just inching our way here that eventually we're gonna be able to list
out the completed to DOS.
So now that we're back inside of the views, we've got to import that serializer that we had just created.
So let's go ahead.
Ensay from DOT.
Serializer is import and this is where we want to to do serializer.
There it is.
Thank you, Adam, for the autumn autocomplete there.
And then we'll say, OK, the serializer class that we're looking for is the to do serializer.
Perfect.
Now, really, the last step that we have here is that we want to make sure that when someone is accessing
this API that they are logged in and that we're only getting objects for that logged in user now to
make sure that someone is logged in.
We can say, OK, the permission class on this permission underscore class plus.
Permission.
There we go.
Class is equal to an inside of the list here, we want permissions.
Which we are going to have to import.
So go ahead and move back to the top here.
After generics, we want to import permissions.
OK, so then we're going to come back down here and say that we want the permissions.
That is authenticated.
So that makes sure that, you know, anyone that's going to be able to list out the completed to DOS
is a signed end user.
And then on top of that, we've got to make sure that they're only get the getting the tattoos that
are relevant to them.
So we've got to define the query sets.
We're going to say new function here.
Def get underscore query set and make sure that you spell this.
Exactly.
Get underscore query set because this is the special name that the Jenko rest frame will framework will
look look for.
Let's pass in self with the colon.
And first we want to get the user object.
So we're going to say user is equal to self dot request dot user.
That's how we know who's looking here.
We want that to be a lower case.
You then we need to go get D to do objects that are connected to this user and have been completed.
So we're gonna do our capital to do which we need to import this as well.
So make sure with this one that you do from two do dot models and then import the capital to do.
OK, so are going to say to do the objects dot and then this is where we want to say filter.
Now the big one here is to make sure that user is equal to user that we just pulled here.
Right.
To say, you know D should be connected there.
The other one is we want to make sure that we get the completed items.
So here we're gonna say.
Date completed.
Underscore, underscore is null and set this equal to false.
So this way it says, all right.
Give me back all the to DOS connected to this user and ones where there is some value inside of the
date completed.
That's how we incited the to do app have decided to find those completed objects.
In fact, a lot of the code that we'll be using here is a lot of the same code that you'll find inside
of the used up high inside of the to do weps.
For example, if we go scroll down to where we get the completed tatoos here, you can see we've got
the same user is equal to the requests dot user and date completed.
Underscore is known as equal to false.
Right.
We've got all that stuff.
And in fact, look, here's a little bonus.
Let's make sure that we order these by.
Which ones have been completed soonest.
OK.
So we'll go ahead and copy that.
Come in.
Pace that there.
And then we need to make sure that we return what this query gets us.
OK.
So I hope that we've gone so many places here, but we're getting really close.
So let's go ahead and save this.
We now should have the ability to go to the API slash Tudou slash completed.
This is the first time we're finally testing our code.
It took a little bit of time to get up and running, but it's all gonna be worth it here.
So let's make sure that we get our server running.
So Python three managed up pie run server.
So that's up and running.
Let's go ahead, you can see currently we have you know, we can see the completed stuff, so it should
be showing by cheese and our API.
But let's go to our Web site, slash API slash to dos slash completed.
OK, so once you come here, you see a very familiar face in the API here.
And look at this.
It has all of our information here.
It's got, you know, the buy cheese, get enough for three pizzas, all of this good stuff, except
that this is not what we're looking for.
Is that the one that we completed now?
That is the one that we completed.
So, OK.
I thought it was the other one.
But this is correct.
I was just caught up for a second there.
So this is great.
The one thing that you'll notice here is I don't have the ability to log in and out with the API.
Remember, if you do want to add that, you can do the API dash off.
But the cool thing is we have a functioning Web site where people can log in and log out.
So, for example, if I come here and I log out and then I try to go back to the, you know, API to
DOS completed, I get this real nasty error.
Now, this is because of a spelling mistake I had instead of my views that pie.
So if we go back to our used up pie, I have permission class.
It should be class is I should have noticed this because it was looking for a list of things.
But if we go ahead and save this and now come back to this page and we reload, we get a much better,
you know, four or three code saying, hey, you can provide any authentication.
I can't show you this.
But so we're we're glad to know that that's what's showing up.
You know, if they're not signed in.
But I'd like to really confirm and show you that this is working.
So let's go ahead and sign back in here.
So go back to our home page.
And then we want to log in.
Let's sign in as Nick and let's go ahead and do a password here.
Let's see if I can remember it.
Yes, I did.
But let's go now to our API.
Go to the completed and now it's showing.
Give yourself a nice pat on the back.
You have the ability to show and list out completed to do items just to really drive the point here.
Let's go ahead and let's create another to make our.
First API you URL.
Okay, so we have now completed this are going to hit complete, we should be showing two completed
things here.
So let's go back to API slash.
Oh, and look at this.
We've got the things here.
It's showing which ones were completed first.
We are in an excellent, excellent position.