Django 用户关注取消关注查询
我正在创建一个博客帖子应用程序,我的要求之一是用户可以相互关注和取消关注。 我为每个用户创建了一个配置文件模型类
,并使用多对多字段将以下
列添加到用户模型
中。
人物档案模型->
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 = 1
和 people_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查是否存在
People
对象,其user_id
为user_id
以及我们在以下
一个撰写该帖子的用户
:You can check if there is a
People
object with asuser_id
theuser_id
and where we arefollowing
aUser
that has written that post:这是我目前拥有的数据库。这里我有一个 people 表、people_following 表、post 表和 user 表。
这是 poeple_following 表的架构。
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.