From c81d7cc7b4b0b4218238771d089100a15d6365d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oktay=20=C3=96zel?= Date: Sun, 24 Nov 2024 22:02:09 +0300 Subject: [PATCH] bookmark fix try2 --- backend/app/models.py | 3 ++- backend/app/serializers.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/app/models.py b/backend/app/models.py index fd3354ea..63da6c64 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -131,6 +131,7 @@ class Post(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts') like_count = models.IntegerField(default=0) bookmarked_by = models.ManyToManyField(User, related_name='bookmarked_posts', blank=True) + liked_by = models.ManyToManyField(User, related_name='liked_posts', blank=True) def __str__(self): @@ -198,7 +199,7 @@ def __str__(self): class Bookmark(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookmarks') - post = models.ForeignKey(Post, on_delete=models.CASCADE) # No related_name needed here + post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='bookmarked_by') created_at = models.DateTimeField(auto_now_add=True) class Meta: diff --git a/backend/app/serializers.py b/backend/app/serializers.py index 81004f0e..ce2b53d9 100644 --- a/backend/app/serializers.py +++ b/backend/app/serializers.py @@ -22,10 +22,12 @@ class Meta: def get_posts(self, obj): """Get posts created by the user, similar to get_post_details.""" posts = Post.objects.filter(author=obj.user) + user = self.context['request'].user + post_data = [] for post in posts: is_liked = post.liked_by.filter(id=self.context['request'].user.id).exists() - is_bookmarked = post.bookmarked_by.filter(id=self.context['request'].user.id).exists() + is_bookmarked = Bookmark.objects.filter(user=user, post=post).exists() comments_data = [ {