我是 django 的新手,正在尝试构建社交网络系统。在我看来,我正在使用配置文件变量导入我的朋友列表。现在我只想显示我朋友的帖子。但无法制定如何过滤所有帖子的逻辑。对于每种方法我都会遇到不同类型的错误。这是观点:@login_requireddef post_comment_create_and_list_view(request): profile = Profile.objects.select_related('user').get(user=request.user).get_all_friends_list() #profile contains list of the friends i have queryset = Post.objects.all_posts().filter(author__icontains=profile.values()) #post form post_form = PostModelForm() if 'submit_p_form' in request.POST: post_form = PostModelForm(request.POST or None, request.FILES or None) if post_form.is_valid(): instance = post_form.save(commit=False) instance.author = profile instance.save() post_form = PostModelForm() messages.success(request, 'Post published') return redirect('posts:main_post_list') #comment form comment_form = CommentModelForm() if 'submit_c_form' in request.POST: comment_form = CommentModelForm(request.POST or None) if comment_form.is_valid(): instance = comment_form.save(commit=False) instance.user = profile post_id = request.POST.get('post_id') instance.post = Post.objects.get(id=post_id) instance.save() comment_form = CommentModelForm() return redirect('posts:main_post_list') context = { 'queryset': queryset, 'profile': profile, 'post_form': post_form, 'comment_form': comment_form, } return render(request, 'posts/main.html', context)这是帖子模型:class PostManager(models.Manager): def all_posts(self): posts = Post.objects.prefetch_related('author',"comment_posted__user", "comment_posted__user__user",'likes') return posts我想要应用的逻辑是从我的朋友列表中获取所有帖子。并非来自每个用户。我应该使用什么方法
1 回答
炎炎设计
TA贡献1808条经验 获得超4个赞
请更正post_comment_create_and_list_view中的author_in:
queryset = Post.objects.all_posts().filter(author__in=profile)
添加回答
举报
0/150
提交
取消