我有一个这样的模型:
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))
有没有办法做到这一点?