Adding the Like Feature
Learn to implement the like feature functionality in our application.
We'll cover the following...
A nice feature to have in a social media application is liking. Like Facebook, Instagram, or Twitter, we’ll allow users here to like a post.
We’ll also add data to count the number of likes a post has received and check whether a current user making the request has liked a post.
We’ll do this in four steps:
Add a new
posts_likedfield to theUsermodel.Write methods on the
Usermodel to like and remove a like from a post. We’ll also add a method to check whether the user has liked a post.Add
likes_countandhas_likedtoPostSerializer.Add endpoints to like a post and remove a like from a post.
Great! Let’s start by adding the new fields to the User model.
Adding the posts_liked field to the User model
The posts_liked field will contain all the posts liked by a user. The relationship between the User model and the Post model concerning the “Like” feature can be described as follows:
A user can like many posts.
A post can be liked by many users.
Does this kind of relationship sound familiar? It is a ...