So let's talk about a situation where a user up voted something, but they take it back.
They actually didn't want to vote for that particular thing.
How do we handle that in our API?
Essentially, we need to be able to delete the vote object.
But how do we do that?
Well, what we can do is go back to our code here and we could create.
If we go to our views, a completely separate class based view that, you know, instead of the create
API view, it's going to be I believe the term is the destroy API view, but we have a much simpler
way that we can do this.
We can use a mix in to add a function animal, implement that.
You know, delete functionality for us.
So how to handle this is we have to be able to use mixin some.
We've got to import this.
So let's scroll to the very top here.
And just after permissions, we're going to add mix ins.
OK.
And we're going to come to our vote, create and say that this also adheres to this is where we want
to do Mexicans dot and then we want to do the destroy.
Model Mixin.
OK, so now that we have this, we'll go ahead and save that.
And the way to implement the code, to destroy something, you could just, you know, go ahead and
leave it as is.
But I think it's best if we have a little function here.
So I'm gonna go ahead and get down and create a new line.
So we want to make a new function here.
And this is going to be delete.
OK, and here's the different parameters that we need, we need self.
We need the request.
We need the Star ARGs and we also need these star star.
OK.
W ARC's awesome.
And a colon here and then basically we need to be allow someone to delete their upvote.
Now the reason that we needed to implement this function is because we don't want them to be able to
delete something that doesn't exist if they've never upvoted something.
There's no vote object in the database, so there's no point for that.
So similarly, like we did above.
We need to check and see if a vote exists in the database.
We already have the code for that.
So going to copy this line where we say, you know, if this thing exists inside of the database K.
So if a vote exists for this particular user on this particular post, then we will allow them to delete
that.
We're gonna say self get query set, dot delete.
OK.
That'll go ahead and get rid of it.
And then we want the user to know that we that the delete was successful.
So we are going to return a response.
OK.
And we've got to import the code for this.
Let's go ahead and scroll to the top.
And this is something else from the rest framework.
So we're going to say from Rest. underscore framework.
DOT response.
We want to import capital response.
So now we're going to return this response object in this response object.
Basically, we can pass forward, you know, a particular code to them to say, you know, this is what
happened.
So we're gonna save status, which remember, there's all those status codes.
And in this case, we want the status.
Dot, I want status is something that we got to import, so let's go ahead and scroll back to the top
here.
After mix ends, we're going to import status.
And we're going to say that we want to return back.
H TTP underscore two oh four underscore no underscore, calm, all caps content.
OK, so that is the status code to say, hey, there was nothing here.
So, you know, we got rid of whatever it is that you were trying to get rid of.
So that's if a vote exists.
What if a vote does not exist?
Well, that's our ELTs.
And in that situation, we want to return or raise a validation error, just like we had before.
So I'm going to copy and paste this.
But rather than saying you early voted, we're gonna say, you know, something like you never voted
for this post, silly thing.
It's kind of fun to work with your API because the only the developers are gonna see this, the users
never gonna see this information unless the developer somehow leaks this in their app or whoever is
using your API.
But anyway, it's kind of fun, I think, to put this kind of thing in there.
So with this in place, let's go back and test this out.
So if we reload the page here, you'll notice look what popped up, this awesome delete button.
So the zappy code user, he can delete his vote just like this.
And look, it is gone, and the way that we know that it's gone is if I can now make another vote.
So if I hit posts, which is essentially creating a vote, I can do that.
And it gives me an idea, meaning that it's added a vote to the database.
But if I try and do it again and again.
Well, it's it's, you know, not going to allow me to do that anymore so I can successfully do that.
And if I delete once.
OK.
And then I come back and I try and delete again.
It's gonna give me that area to say, hey, you never voted for this post, silly.
I can't deleted if it's not there.
So.
And the way to really see that this is all working and this is really going to start tying things together.
Let's go back to the post list.
Right.
If someone wants to see all the different posts.
Yes.
It's cool to see the title in the You Are.
But we want to know how popular things are right now.
Nothing is being listed for how popular things are.
How many votes that they got.
So if we go ahead and go back to our code, we can implement inside of our serializer as pi inside of
the post serializer.
A way to go get all the different votes that someone has.
So we're gonna say that votes is equal to Serializer is with the S dot capital.
Serializer.
Make sure that that's capital serializer method.
Field.
So that means, you know, we want to include votes inside of here as something that's going to be,
you know, returned back, but the way that the information will be generated is through a function.
And so down here, still inside of this post serializer class, we're going to say, let's make a function
called get.
And then underscore votes.
So basically, the name of your function has to be the same name of the variable that we have here.
So get underscore whatever the name your variable is.
And then inside of our get votes, it's going to have self and then also an object of the post object.
And we're going to say here that we want to return.
Capital vote dot objects.
Basically, we want to go see how many vote objects there are for this particular vote.
So that's what we're gonna say.
Vote not objects.
Dot filter.
And say, give me all the vote objects for this particular posts or against a post is equal to post
and then we just want the count of this.
All right.
So with just this piece here, if I go ahead and save this, I didn't have to change anything inside
of my views.
It's all handled via the serializer.
If I come back and I reload the list of all these posts.
It doesn't work.
Let's see, we got hit rest frame worked out, serializer is has no attribute serializer of misspelled
method field.
OK.
So it's telling me this at line seven of the serializer is the PI.
So it's got a serialized pipeline seven.
Let's change this to Method Field.
OK.
Let's save this.
And now if I try and load this page, there we go.
And look at this.
We have the votes.
So everything has no votes except for a hole.
The very first post, which does have one vote currently from Nick.
But it doesn't have one from zappy.
So this will be a great way to show that our API is working because I'm logged into zappy code.
I'm going to go to the one post slash vote.
I'm going to then vote.
And look, it's generated.
Give me the tool one to say that it's created something.
Now, if I go back to the post, let's look at that.
Now, two upvotes for this particular post.
Oh, you got to just be feeling great about what's going on here.
We are getting really close to be done with our API.
Really, the next step for us here is to allow someone who's made a post to say, you know what, I
didn't really want to post that.
How do I delete a post?