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 に答える