Now, the next step for us on our site is add the ability for someone to be able to complete a to do
so.
For example, in my current to do list that one of the things that I want to do is complete it to do.
How do I say that I have accomplished this?
Well, it starts first by creating a U.
RL, just like we've done all these other times.
Now it's going to be very similar to what we've done.
If someone wants to update or delete an existing to do, we're just going to say to do slash get the
primary key.
But then we're going to say the user should pass in complete.
OK.
So if someone says I want to complete to do number five, they're gonna do to slash five, slash complete,
which then we need to go create an appropriate view for this.
Let's go ahead and move over to our views.
Dot Pi.
And this is just going to be an update for you.
So let's go ahead and copy what we had up above here for the to do retrieve, update, destroy and really
simplify this down.
So we're going to change the name, the name of this to be to do complete.
Then we're gonna change this to just be the update API view.
Now, with this in place, it's very tempting to use the current serializer that we have.
But unfortunately, that's not going to work for us, because when someone is going to update to say,
hey, I want to complete this particular to do, there's nothing, in fact, on the model that we want
them to be able to mess with.
If we go to the to do models, we don't want them to change the title.
The memo that created the date completed important or the user in hear me clear on this.
We do not want them to be able to set the date completed.
That's something that should be handled by our system.
So what we're going to do is we're gonna come back to this to do completed and rather than using the
classic to do serializer that we've been using everywhere.
Let's make a new serializer.
It's gonna be very similar.
So we can, in fact, copy a lot of what we have here and paste it down below.
And I'm going to call this the to do complete serializer.
OK.
And what we're gonna change here is that we want all the fields to be read only.
So what I'm going to do is I'm going to delete these two fields that we said we're gonna be read only
instead of just saying, you know, these are all the fields.
These are gonna be the read, underscore only field.
So all of these are going to be real read only.
Now, there is one field that we still want to hold on to.
So are going to say Fields is equal to the I.D..
And go ahead and delete that from the list down below.
So that's the one that we're gonna keep around.
But everything else is going to be read only fields.
So we could have just manually typed all those out there.
But I think this one looks a whole lot cleaner and now we have this to do complete serializer.
So with this, I'm going to go ahead and save the serializer, stop pi, come back to my views, import
this up at the top.
So I'm going to do comma to do complete serializer and now pass this through to say, OK, I want to
use this to complete serializer.
So now that we have a name for this class, let's go ahead and copy this.
Come back to our your LS and update that there.
Save that file and let's come back and finish the view here.
So again, the user shouldn't be passing us any information.
They've already passed the I.D. of the particular to do that they want to finish.
So what we've got to do is add a custom function here when someone is going to do the updates or and
say def perform, underscore, not create.
We want update and inside of here we're going to pass self and also the serializer.
We go through our colon and then inside of this function, basically, we want to set the date completed
to the current time and then save that serializer.
So what we're gonna do is we're going to get our serializer dott instance.
So this gets the particular object that we're working with.
Remember, it already exists in our database.
And then we're going to take it's date completed and we're gonna set this equal to and we want to use
a time zone which we're gonna have to import this code.
So let's go ahead and scroll up to the top of our file here.
And in order to get the timezones here, this comes from Django.
We're going to say that we want to say from Django spelled Django correctly, from Django Dot Utils.
OK, we want to import time zone.
Excellent.
OK.
Now that we've got that in place, we can say time zone dot now, which just takes the current time
and sets that there.
And once that's in place, we basically just want to save that object.
With this new completed dates, we're going to say our serializer dot save with open and closed parentheses
there.
And I realize here that I have misspelled perform, so I need per form update.
Okay, let's go ahead and save that.
Come back to our project and let's go ahead and, you know, say, hey, I want to complete.
Number five here.
So I'm going to go say to dos slash number five, slash complete.
OK.
Now here it looks like it's not happy with our serializer.
And let's go ahead and look inside of our project.
Inside of the serializer is that pie.
Now, look at this.
I have horrific spelling.
So let's do fields sort of flails or whatever I had spelt there.
OK, so let's save this comeback.
Reload this page.
OK.
So it's telling me I can do a put or a patch here for this, you know, to do number five here.
So it's saying hey, you can't do a get.
But great thing about the jingle rest framework is it sort of helps people to understand what your API
is able to do.
So in this case, if I simply just do the put or a patch, I do a put here, it's going to market as
complete.
So I'm gonna do that.
It gives me the 200.
OK.
So now if I go back to my listed to DOS here, you'll notice that I no longer have that number five
there.
I just have the one that says add an API to my site, which we're not done with that yet.
But if I go to my API and I say I want to see my completed look at that completed to do, we just did
that via the API.
If you want to confirm, we can go to our completed here via the Web portion here and see that beautiful
to do.
Oh, it's so awesome that we've got all this functionality in place.
So at this point we are basically done with the to do app.
It can do everything that we want it to do.
We can, you know, see the current ones, the completed ones that we can create.
If we have an ape to do, we can, you know, update it, we can complete it.
We can delete it.
All these different pieces here, really the only thing that we're missing is how does someone even
access the API with, you know, their current logged and credentials like the current user, Nick.
How does Nick use this if he's not going to be using the Web browser here, like if he was on a mobile
app, like if we were going to use Kerl?
How would that happen?
And, you know, how do we allow someone to sign up via our API?
These are all questions that we're going to answer but know that the to do functionality is all complete
up till this point from here on out, it's really just authentication.