[Django]-Show user posts in post list

3👍

The reason this does not work is because, likely get_following() returns a QuerySet, not a list, and you can not append to a Queryset.

Likely it is easier to just implement with disjunctive condition:

from django.db.models import Q

def get_queryset(self, *args, **kwargs):
    following_users = self.request.user.profile.get_following()
    return Tweet.objects.filter(
        Q(user__in=following_users) |
        Q(user=self.request.user)
     ).order_by('-timestamp')

2👍

You need to use Q objects, so you look for posts from users that you follow OR from yourself

from django.db.models import Q


def get_queryset(self, *args, **kwargs):

    # returns the users that i follow
    following_users = self.request.user.profile.get_following() 

    #gets posts of following users
    qs = Tweet.objects.filter(Q(user__in=following_users) | Q(user=self.request.user)).order_by("-timestamp") 

    return qs

Leave a comment