Django 用户关注取消关注查询

发布于 2025-01-14 19:27:50 字数 1720 浏览 2 评论 0原文

我正在创建一个博客帖子应用程序,我的要求之一是用户可以相互关注和取消关注。 我为每个用户创建了一个配置文件模型类,并使用多对多字段将以下列添加到用户模型中。

人物档案模型->

class People(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    following = models.ManyToManyField(to=User, related_name='following', blank=True)
    photo = models.ImageField(upload_to='profile_pics', blank=True,null=True)
    Phone_number = models.CharField(max_length=255,null=True,blank=True)
    Birth_Date = models.DateField(null=True,blank=True)
    Created_date = models.DateTimeField(auto_now_add=True)
    Updated_date = models.DateTimeField(auto_now=True)

现在,我列出了网页上的所有帖子,并且模板上还提到了每篇帖子的作者。要求是我需要提供一个按钮来关注或取消关注帖子作者。 现在,如果用户第一次进入页面,如果帖子的作者已经被关注,我需要显示一个取消关注按钮,为此,我需要检查每个帖子作者的关注并根据以下内容对我的模板进行更改来自数据库的响应。

这是我写的查询 ->

posts =  Post.objects.exclude(users=request.user)\
    .select_related('user__people','ProductAvailability').prefetch_related('images_set','Likes')\
    .annotate(comments_Count = Count('comments_post',distinct=True)).annotate(
        Count('Likes',distinct=True),is_liked=Exists(
        Post.Likes.through.objects.filter(
            post_id=OuterRef('pk'), user_id=user_id
        )
    ),isSaved=Exists(
        Post.favourites.through.objects.filter(
            post_id=OuterRef('pk'), user_id=user_id
        )),
        isFollowed=Exists(
            People.following.through.objects.filter(
                people_id=OuterRef('pk'), user_id=user_id
            )
        )
    ).all().order_by('-id')

现在数据库显示了 user_id = 1people_id = 3 的记录,即用户 1 关注了 id 3 的人。 但上面的查询返回 0 作为 isFollowed 中的输出。

有人可以指出我犯的错误吗?

I am creating a Blog post application and one of my requirements is that users can follow and unfollow one another.
I have created a profile model class for each user and in that I have added the following column to the user model using a many to many field.

People Profile Model ->

class People(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    following = models.ManyToManyField(to=User, related_name='following', blank=True)
    photo = models.ImageField(upload_to='profile_pics', blank=True,null=True)
    Phone_number = models.CharField(max_length=255,null=True,blank=True)
    Birth_Date = models.DateField(null=True,blank=True)
    Created_date = models.DateTimeField(auto_now_add=True)
    Updated_date = models.DateTimeField(auto_now=True)

Now I am listing all the posts on a webpage and for each post the author is also mentioned on the template. The requirement is that I need to give a button to follow or unfollow the post author.
Now for the first time if the user comes on the page on if the author of the post is already followed that I need to show an unfollow button and for this I need to check every post author followings and make changes to my template as per the response from the DB.

This is the Query that I have written ->

posts =  Post.objects.exclude(users=request.user)\
    .select_related('user__people','ProductAvailability').prefetch_related('images_set','Likes')\
    .annotate(comments_Count = Count('comments_post',distinct=True)).annotate(
        Count('Likes',distinct=True),is_liked=Exists(
        Post.Likes.through.objects.filter(
            post_id=OuterRef('pk'), user_id=user_id
        )
    ),isSaved=Exists(
        Post.favourites.through.objects.filter(
            post_id=OuterRef('pk'), user_id=user_id
        )),
        isFollowed=Exists(
            People.following.through.objects.filter(
                people_id=OuterRef('pk'), user_id=user_id
            )
        )
    ).all().order_by('-id')

Now the DB shows the record of user_id = 1 with people_id = 3 i.e user 1 follows people with id 3.
but the above query returns 0 as output in isFollowed.

Can someone point out the mistake I am making.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2025-01-21 19:27:50

您可以检查是否存在 People 对象,其 user_iduser_id 以及我们在以下一个 撰写该帖子的用户

posts = Post.objects.exclude(users=request.user).select_related(
    'user__people','ProductAvailability'
).prefetch_related('images_set','Likes').annotate(
    isFollowed=Exists(
        People.objects.filter(
            following__post__id=OuterRef('pk'), user_id=user_id
        )
    )
).order_by('-id')

You can check if there is a People object with as user_id the user_id and where we are following a User that has written that post:

posts = Post.objects.exclude(users=request.user).select_related(
    'user__people','ProductAvailability'
).prefetch_related('images_set','Likes').annotate(
    isFollowed=Exists(
        People.objects.filter(
            following__post__id=OuterRef('pk'), user_id=user_id
        )
    )
).order_by('-id')
久伴你 2025-01-21 19:27:50

输入图片这里的描述

这是我目前拥有的数据库。这里我有一个 people 表、people_following 表、post 表和 user 表。

这是 poeple_following 表的架构。

输入图片此处描述

enter image description here

This is the DB That I have currently. here I have a people table, people_following table, post table and a user table.

and this is the schema for the poeple_following table.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文