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

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

我正在开发一个 Django 项目,用户可以在其中关注其他用户、创建帖子等,就像 Twitter 或 Instagram 一样。但是,我在 views.py 中编写查询以过滤掉当前登录用户正在关注的人员的所有帖子时遇到问题。在这里,我分享我的views.py函数、模型、模板和对应的ER图。

views.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

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")

我按照下面的ER图设计了我的数据库。

urls.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

<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>

但是当我访问 following.html 时,它只显示一个用户的帖子(后面是当前登录的用户)并且不显示其他人的任何帖子。我现在该怎么办?

试试这个

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

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

这是每次都给 posts 变量赋值,所以你只会得到最后关注的帖子。

只需执行类似的操作并删除循环

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