0

I have a model like this:

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)

And now in my view I want to filter so that the logged in user can se all Posts that the person follows. And the problem is that more people can follow the same Post.

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))

Is there a way of doing this?

avasal
  • 14,350
  • 4
  • 31
  • 47
Fredrik
  • 3
  • 1

1 Answers1

0

Have a look at the docs.

You should be able to do do -

Post.objects.filter(followers__in=[user])
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • Thanks! I tried that earlier but it did not work for some reason. But when I tried again it did. – Fredrik Feb 22 '13 at 10:56