Anoop K George
Anoop K George

Reputation: 1755

Like button back end logic using Django Rest Framework

I have a Blog app where users can add new Post and any users can like the post.

models.py

class Post(models.Model):
    title = models.CharField(max_length=60)

class PostLikes(models.Model):
    likeusers = models.ManyToManyField(User)
    likepost = models.ForeignKey(Post,on_delete=models.CASCADE,null=True,related_name='likepost')

serializers.py

class PostSerializers(serializers.ModelSerializer):

    #likepost = serializers.SerializerMethodField() **I have to mention like count here**
    class Meta:
        model = Post
        fields = '__all__'
class PostlikeSerializer(serializers.ModelSerializer):
    class Meta:
        model = PostLikes
        fields = '__all__'

views.py

class LikeListCreate(APIView):

    def get(self,request,pk):#function to get total number of likes to particular post
        post = Post.objects.filter(pk=pk) # find which post's likes are to be extracted
        like_count = post.likepost.count()# counts total user likes ,besides my code is wrong
        serializer = PostlikeSerializer(like_count,many=True)
        return Response(serializer.data)

    def post(self,request,pk):#function to add likes to post
        # how do I check if user is already liked the post ?
        likeusers = request.user
        likepost = Post.objects.filter(pk=pk)
        serializer = PostlikeSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(likeusers,likepost)
            return Response(serializer.data,status=status.HTTP_201_CREATED)
        return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)

urls.py

    path('posts/<int:pk>/like/',LikeListCreate.as_view(),name = 'post_likes'),

I am unable to implement logic in views.py module and serializers.py section too. Please help, thanks in advance

Git link to my project https://github.com/Anoop-George/BlogApp.git

Upvotes: 1

Views: 2274

Answers (2)

rizwan ali
rizwan ali

Reputation: 11

def post(self,request,pk):
    likeusers = User.objects.get(id=2)// user find for testing purpose
    likepost = Post.objects.filter(pk=pk)
    check = PostLikes.objects.filter(Q(likeusers=like_user) & Q(likepost = likepost.last() ))
    if(check.exists()):
        return Response({
            "status": status.HTTP_400_BAD_REQUEST,
            "message":"Already Liked"
            })
    new_like = PostLikes.objects.create(likeusers=likeusers, likepost=likepost.last())
    new_like.save()
    serializer = PostlikeSerializer(new_like)
    return Response(serializer.data,status=status.HTTP_201_CREATED)

Upvotes: 1

0sVoid
0sVoid

Reputation: 2663

You're serialising the request data but you've already extracted data from the request to get the user and the post, I would therefore suggest using that data to create the PostLike in your views.py in a standard way:

new_like = PostLikes(likeusers=request.user, likepost=likepost)
new_like.save()

I would also add that models are usually titled as singular items, i.e. PostLike and not PostLikes - you can refer to postlikes using the related_name of the ForeignKey field.

Upvotes: 1

Related Questions