[英]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.