0

我有一个这样的模型:

from django.contrib.auth.models import User

class Post(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    followers = models.ManyToManyField(User, null=True, blank=True)

现在在我看来,我想过滤,以便登录用户可以查看该人关注的所有帖子。问题是更多的人可以关注同一个帖子。

def get_followed_posts(request):  
    user = request.user  
    posts = Post.objects.filter(followers__contains=user) # If the user is in the list of followers return the Post.
    return render_to_response('post_list.html', {'posts': posts}, context_instance=request_context(request))

有没有办法做到这一点?

4

1 回答 1

0

看看文档

你应该能够做到 -

Post.objects.filter(followers__in=[user])
于 2013-02-22T10:47:58.670 回答