On 1 juil, 14:44, coan <a.ne...@gmail.com> wrote: > I have a friendship model for users and want to retrieve all the posts > my friends made. > > class Friendship(models.Model): > from_friend = models.ForeignKey( User, related_name='friend_set' ) > to_friend = models.ForeignKey( User, related_name='to_friend_set' ) > > class Post(models.Model): > text = models.TextField() > user = models.ForeignKey(User) > > I retrieve my friends posts with this: > > my_friendships = Friendship.objects.filter(from_friend=request.user) > list_of_my_friends_ids = [] > > for friendship in my_friendships: > list_of_my_friends_ids.append(friendship.to_friend.id) > > posts_my_friends_made = Post.objects.filter( user__in = > list_of_my_friends_ids ) > > Is there a better, less ugly way to filter the posts?
I don't know what Django can do here, but with Python you can do : friends_ids = [ f.to_friend.id for f in Friendship.objects.filter (from_friend=request.user) ] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---