In one of my views I override the get_queryset(self) method in order to return a list of users that excludes the friends of the user making the request and the same user. This is what my code looks like:
def get_queryset(self):
all_users = User.objects.all()
user_friends = Friend.objects.friends(self.request.user)
# Filter all_users to exclude all elements of the user_friends queryset.
# Filter all_users to exclude the current user making the request.
# Return the filtered queryset.
I am using the django-friendship library to handle friend requests and users being friends. In the getting data about friendship section, it says to get a list of all a user's friends all that needs to be done is Friend.objects.friends(request.user)
. I am unsure if this would return a list of Friend instances or of User instances. The source code for the Friend model is here. In either case, how would I filter the queryset all_users
to exclude all elements of the list user_friends
.
Friend
s haveto_user
andfrom_user
attributes. When you say you want to filter theall_users
QuerySet
, should that be based on theto_user
or thefrom_user
values inuser_friends
? – Chris