Creating Python APIs - The Django REST framework. Building a Reddit clone - Deleting Posts.[9/n]

Play this article

Congratulations! Now you're on the final part of successfully creating your very own basic reddit clone.

Now for whatever reason, if the user wants to delete his post he should be able to. Let's give him that ability.

We need an url for that, so we go to the file and add the url

path('api/posts/<int:pk>', views.PostRetrieveDestroy.as_view()),


Notice the new view, PostRetrieveDestroyView which we have not defined yet. We will do that in the next.

You can read about PostRetrieveDestroyView here, and if you go through the documentation you'll also notive there's another view RetrieveUpdateDestroyAPIView which might seem more suitable for this. Basically PostRetrieveDestroyView gives you an option to view and delete a post and RetrieveUpdateDestroyAPIView gives you an option to view, update and delete functionality. As you know on Reddit, you cannot edit posts, you have delete your post and create another one. Twitter follows the same, it's because if you create a sensible post that becomes viral, you would have the ability to edit the post to be something unsavory.

Now, we add the PostRetrieveDestroyView, you can use RetrieveUpdateDestroyAPIView as well if you want the users to be able to edit their posts.


Since it is Post, we can use the same options in class PostRetrieveDestroy as we had in class PostList

Now if you save this and refresh the webpage, this works. But there is a caveat, let me show you:

These are the current posts we have, as you can see we have 3 posts and we are logged in as the user Saurav.


What if I want to delete the 2nd post, let's try deleting it.

We'll use the url http://localhost:8000/api/posts/3

Notice the ID for the post is '3', although the post is 2nd. Be careful with the IDs, they should be the one in url and not the post number.


Now click on delete, it will ask if you absolutely want to delete the post. Click on delete.


Now if you go to the main page with all the posts, you should see only two posts.


Let's login as the other user we created(gaurav) and try to delete a post created by him.


If you come to the main page now and refresh, you should be able to see all the posts created by Saurav and Gaurav on the same page.

Let's try to delete the post we just created using Gaurav's ID.


Notice Gaurav's post ID is 8, make sure you put the correct ID in the url. The url should be http://localhost:8000/api/posts/8

Now, click on delete and voilà!


The post is successfully deleted, you can check on the homepage as well.


So it is successful, we can delete the post. and it works for all users as well.

Something that is an issue is if you try to delete another user's post. Let's say Gaurav wants to delete a post Saurav created. What would happen?


Gaurav wants to delete the 3rd post by Saurav. Let's see if he can delete. What do you think should happen?


It got deleted!


There's only one post that remains now.

Why do you think Gaurav was able to delete Saurav's post?

Think, we've already covered this.

Gaurav is able to delete Saurav's post is because although we've set the permission as "Authenticated or Readonly". Which means to delete the post you need to be authenticated, or you have only read access. We do not check if the user that is making the delete request is the same as the one currently logged in.

Let's fix that!

Add a delete function for the PostRetrieveDestroy class.


It is very straightforward, you define a post variable where you assign the primary key to that of the post and the poster to be the user making the request. If post exists, meaning the user logged in is the user that has created the post with the id, then we invoke the destroy function, else we raise a validation error.

To test this works, I've created a new post logged in as user Gaurav, now let's try to delete it logged in as Saurav.


The id for the post is 9, so the url should be http://localhost:8000/api/posts/9


Let's try to delete that post.


Look at that! It is working perfectly now. It crosschecks the user that is logged in with the user that is making the request.

How awesome is that? Congratulations! You have successfully created a basic reddit clone.

Next, we will add an API to an existing project.