简体   繁体   English

我无法在 django 中进行查询以过滤掉当前登录用户正在关注的那些用户的帖子

[英]I can't make query in django to filter out posts from those users that the currently logged in user is following

I am working on a Django project where a user can follow other users, create posts and so on just like Twitter or Instagram.我正在开发一个 Django 项目,用户可以像 Twitter 或 Instagram 一样关注其他用户、创建帖子等。 However, I have a problem writing queries in views.py to filter out all the posts from the people that currently logged in user is following.但是,我在 views.py 中编写查询以过滤掉当前登录用户正在关注的人的所有帖子时遇到问题。 Here, I am sharing my views.py function, model, template and corresponding ER diagram.在这里,我分享一下我的views.py function,model,模板和对应的ER图。

views.py视图.py

def followerspost(request):
  user = request.user
  profile = Profile.objects.filter(user = user)
  followings = user.profile.followings.all()
  for following in followings:
    posts = NewPost.objects.filter(user = following)

  return render(request,"network/following.html",{
    "user" : user,
    "profile" : profile,
    "followings" : followings,
    "posts" : posts,
  })

models.py模型.py

class NewPost(models.Model):
  user = models.ForeignKey(User, on_delete = models.CASCADE)
  post = models.TextField()
  timestamp = models.DateTimeField(auto_now_add = True)

  def __str__(self):
    return f"{self.post}"

class Profile(models.Model):
  user = models.OneToOneField(User, on_delete = models.CASCADE)
  picture = models.ImageField(blank=True,null=True,upload_to="images/")
  followers = models.ManyToManyField(User, blank=True, related_name="followers")
  followings = models.ManyToManyField(User,blank = True, related_name="followings")

I have designed my database according to the following ER diagram.我根据以下 ER 图设计了我的数据库。 在此处输入图像描述

urls.py网址.py

urlpatterns = [
  path("profile/<int:id>/following/addfollower",views.followersPeople,name="addfollower"),
path("profile/<int:id>/following/removefollower",views.followersRemove,name="removefollower"),
path("following",views.followerspost,name="following"),]+ static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)

following.html以下。html

 <h1>All Posts</h1> <div id="posts" class="card"> <div class="card-body"> {% for posts in posts %} <ul> <li class="card"> <div class="card-body"> <h5 class="card-title"><a style="text-decoration: none;" href="{% url 'profile' posts.user.id %}">{{ posts.user }}</a></h5> <h6 class="card-subtitle text-muted">{{ posts.timestamp }}</h6> <h3 class="card-text">{{ posts.post }}</h3> </div> </li> </ul> {% empty %} <h6>No post availabel </h6> {% endfor %} </div> </div>

But when I visit the following.html, it displays posts from only one user (Followed by the currently logged in user) and doesn't display any posts from others.但是当我访问下面的html时,它只显示一个用户的帖子(后面是当前登录的用户),不显示其他人的任何帖子。 What should I do now?我现在该怎么办?

try this尝试这个

def followerspost(request):
  user = request.user
  profile = Profile.objects.filter(user = user)
  followings = user.profile.followings.all()
  posts = NewPost.objects.filter(user__in= followings)

  return render(request,"network/following.html",{
    "user" : user,
    "profile" : profile,
    "followings" : followings,
    "posts" : posts,
  })
  for following in followings:
    posts = NewPost.objects.filter(user = following)

this is assigning value to posts variable every time, so you will get only last following's posts.这是每次都为帖子变量赋值,所以你只会得到最后一个关注的帖子。

just do something like this and remove the loop只是做这样的事情并删除循环

posts = NewPost.objects.filter(user__in = followings)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 中获取用户(当前登录的用户)的帖子? - How to get the posts of a user (who is currently logged in) in django? Django - 管理区域 - 我无法从用户中删除用户(用户匹配查询不存在。) - Django - Admin Area - I can't delete a user from users (User matching query does not exist.) django 查询以获取用户的帖子,然后是用户 - django query to get posts from users followed by a user Django Queryset,访问除了那些属于登录用户的所有查询? - Django Queryset, accessing all query except those belongs to logged users? 如何将当前登录的用户传递给filter.py,即在Django中基于请求的过滤 - How to pass currently logged in user to filter.py i.e request based Filtering in django Django:过滤当前登录用户的 ListView(多多多场) - Django: Filter ListView on currently logged-in user (manytomanyfield) 您可以通过instagram api实时查看已通过身份验证的用户的用户帖子吗? - Can you see posts from users that your authenticated user is following in real time with the instagram api? Django模型:从当前登录的用户返回用户名 - Django Model: Returning username from currently logged in user 每次用户在 Django 中登录和注销时,如何登录? - How can I log every time a user has logged in and logged out in Django? 目前遵循官方的Django教程,我无法检查列表的长度是否等于0。为什么? - Currently following the official Django tutorial, and I can't check if the length of list is equal to 0. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM