So let's talk about the situation where someone wants to delete a particular post.
They made a post, but they're saying, you know, I don't really want to do this anymore.
This wasn't a great link.
Whatever it is, let's provide the user that ability.
So if we go to our views, dot pie, we just used a destroy model mix in to be able to add the ability
to delete to an existing view.
So we you might think, oh, well, let's do this for the post list.
That way we can, you know, list, create and also do a destroy.
But the issue is that are you are well currently if we go to you or else that pie for that particular
view is for an API slash post and we don't specify any ideas for what it is that we're looking for.
We do for the vote, but we don't want you know, if someone's using our API, be really confusing for
them to say, you know, hey, if you know you're trying to delete, you use the vote or did the post
and then you have to pass on a number.
It be really confusing.
What we really want is to say, you know, let's create API slash, post slash.
And same thing that we got here, you know, someone passing some sort of I.D. and then we can either
display information about it.
Maybe they can update things about that particular post.
You know, maybe they can delete things.
You can decide what power you want to grant that user, but you've got to decide that.
So let's go ahead and add this particular.
You are l and then a view to back this up.
So because this is a post and we're gonna go ahead and copy what we have for post lists and use a lot
of what we have here.
And you may be saying, OK, we don't want the list, we don't want to create.
Do we just want to destroy?
If we go ahead and look at the Django Rest. framework, if we go inside of the API guide.
And again, this thing is a wealth of knowledge.
But if you go to generic views, you'll see all the different things that we have here.
So, for example, there's the list API view that we first used them the list and create.
There's the destroy model mixin that we use before.
And this one, I think this is the grandmaster, the retrieve update destroy API view.
Basically, this says you can read something about a particular model.
You can update something about it or you can delete it.
And I think those are all valid things for what we want to do.
So I'm going to go ahead and use this retrieve, update, destroy.
And, you know, now this is sort of a decision here because, for example, on the actual Reddit,
you can't update a post that you've created.
You have to delete it and make a new one.
If you want to change the title or the you are RL anything about it.
And I think a lot of the important part of that is, you know, for example, if someone makes a really
popular post and they have it going to a particular, you are well, you know, some sort of scammer
could get something really popular and then switch out the you are l for something really nefarious
right at the end there.
So, you know, maybe it isn't a great idea to do the, you know, update, retrieve, update, destroy.
You know, maybe we just want to use the retrieve, destroy.
Now, I'm thinking this is actually a better move for us.
So let's go ahead and copy this one.
Retrieve, destroy K and we'll come back to our project.
So instead of being this post list, I'm gonna call this post Retrieve, Destroy.
And this is going to use generic stock, retrieve, destroy API view.
So the query set is still going to be for post objects.
We're still going to use the post serializer for this one.
We want to make sure that it is authenticated.
No read only just.
Well, actually, that's not true because we do want someone to be able to see the details for a particular
post.
So we should keep that there.
And because we don't need to do any sort of special thing, when someone deletes a post, we can go
ahead and just delete this, perform, create we you know, you could go add the same sort of delete
function here and do something really custom.
But we don't need to go into that level of detail.
It's all handled for us by the framework.
So cool.
Okay, so I'm going to copy the name of this post.
Retrieve, destroy, save this file.
Come back to our your URLs dot pi and say if someone goes to this post they can, you know, do a post,
retrieve, destroy.
And if we go back to our API and let's say I want to go look at this very first post, I'm gonna go
do that very first post, slash one K.
This is too sad for me to delete this.
In fact, what I want to do is I want to log out of zappy code because that be code has made all the
posts I'm going to do.
One is Nick.
Let me log him in here.
I want to first go ahead and create a new post.
So I'm going to say.
Sports is what adding here, and this is going to go to HTP, colon slash, slash ESPN that come.
Post that now if we go to our post lists, oh, look, there is, you know, sports from Nick, let's
say I was like, I really don't like ESPN.
You know, I want to share Yahoo!
Sports something.
I don't know what it is, and I need to delete this.
Well, now I can go to see the idea of this.
Number five.
So I'm going to go to slash posts, slash five K and let's say I want to delete this.
I hit delete.
It gives me a tool for saying that it has deleted it if I go back to the post list.
Oh look, it's completely gone.
Now something that is an issue.
Let's say I look at number three here and I say, oh, I should go delete whatever is number three.
So I go to number three here and I hit enter and I say I'm going to delete this particular thing.
Currently, I have the power to do that.
Right.
If I go back to the post, Lissa's is like, oh, OK, that's that's not what we're looking for.
So I just told you, we didn't need an additional function.
We actually do need an additional function if we go to have used up PI on a copy.
What we have here for the delete.
We'll go ahead and copy this.
And on this post, retrieved Destroy will say, hey, if someone wants to delete this, we better make
sure that they, you know, have access to this particular one.
So what we're gonna do here is very similar to what we've seen before where we're gonna say, you know,
hey, does this post belong to this particular user?
So we're gonna say, you know, is there a post dot objects dot?
And we want to filter and make sure that this particular post we're gonna pass in first, the primary
keys are going to say P K is equal to the primary key that we get from the KW args.
In fact, we can go ahead and just use that here, OK?
Then we need to say that the poster is equal to.
And we want this to be, you know, whatever users making this request.
We're gonna go ahead and copy this pace that they're awesome.
So we're gonna save this into a little variable called Post and then we'll basically do a little if
statement here.
We're gonna say if post DOT exists, meaning, you know, this thing is in the database.
Well, then we want to do a proper delete.
Now, we could go ahead and make this, you know, use what we did before where we, you know, grab
the thing and delete it, then return response.
There's actually a simpler way that we can do this.
You know, the vote one was a little bit complicated here about how this all came to be.
But with the particular post, we can just simply say return self.
Dot.
Destroy.
OK.
And we have to pass in the request.
And the Star ERGs and also the star star KW ARGs.
OK.
So if it exists, then we'll do the destroying.
This will give back the proper to form method or status code.
I wanted to see the difference between those two there.
But, you know, if that thing does not exist in the database, that means this post doesn't belong
to them.
So this is where we'd want to raise some sort of validation error.
So I would copy this, come back and paste this and I'll say something like, this isn't.
Your post to delete.
Bre.
There we go.
They don't really tell him what's going on.
Got to escape that single parentheses there.
OK.
So let's go ahead and save this and let's come back to our project here.
So let's go to the post.
Listen.
I'm currently signed on as Nick and I see, you know, hey, I don't like this sappy code Web site.
I'm going to delete this stinking thing.
I might be saying to myself, OK, so I'm going to go to post slash one in here, even though I don't
own it.
I can view it, but if I try and go delete this and hit delete says, hey, this isn't your post to
delete, bruh.
But if I.
Nick, go back and create a post like I will say, let's do search engine again.
Google dot com k.
So I've made this this has an idea of six.
So I've go to post slash six K and I want to delete this.
I hit delete.
Boom, it is gone.
It is no longer part of the posts.
So there you have it.
We have completed the project.
I have a few things that I want to talk about as we wrap everything up.
But you should be very proud of yourself.
You just made your very first Django API is a really big deal.