2👍
✅
Your query returns distinct TeamMember
not distinct user
.
In order to get distinct user
:
user.objects.filter(
teammember__team__teammember__user__icontains=query
).exclude(user=request.user).distinct()
Explained: distincts users
that they teammember
has a team
that is related to a teammember
that has a user
that contains query
all this excluding logged user.
Source:stackexchange.com