[Python] How to get mutual followers (mutual friend) for users - Django

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    How do I get mutual followers (friends of friends) with request.user to be displayed for each users posts, I was able to display the count for mutual followers for each users posts on newsfeed. How can I get it done?

    What I tried: I was able to do this but I do not think it is the best way because when I use slice to show three mutual users images it does not work properly for all users.

    This is what I tried:

    class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True,null=True)
    profile_pic = models.ImageField(upload_to='UploadedProfilePicture/', default="ProfileAvatar/avatar.png", blank=True)
    following = models.ManyToManyField(
    'Profile', # Refers to the User model itself
    symmetrical=False, # If A follows B, B doesn't automatically follow A
    related_name='followers', # Reverse relationship: get followers of a user
    blank=True,
    )

    class Post(models.Model):
    poster_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, blank=True,null=True)

    def following_view(request):
    posts = Post.objects.filter(
    Q(poster_profile__profile__followers__user=request.user)).order_by("?").distinct()
    mymutual_followers = request.user.profile.following.filter(id__in=request.user.profile.following.values_list('id', flat=True))

    {% for post in posts %}
    {% for user_following in mymutual_followers %}
    {% if user_following in post.poster_profile.profile.following.all|slice:"3" %}
    <img src="{{ user_following.profile_pic.url }}" class="hover-followers-img"/>
    {% endif %}
    {% endfor %}
    {% endfor %}


    The code above is what I tried and it worked, but add slice:"3" to show just 3 images do not work properly for some users, so I think maybe it's not the best approach.

    The code below is how I got the count of mutual followers and it work perfectly but could not display the mutual followers images; how can I get it done?

    class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True,null=True)
    profile_pic = models.ImageField(upload_to='UploadedProfilePicture/', default="ProfileAvatar/avatar.png", blank=True)
    following = models.ManyToManyField(
    'Profile', # Refers to the User model itself
    symmetrical=False, # If A follows B, B doesn't automatically follow A
    related_name='followers', # Reverse relationship: get followers of a user
    blank=True,
    )

    class Post(models.Model):
    poster_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, blank=True,null=True)

    def following_view(request):
    posts = (
    Post.objects.filter(Q(poster_profile__profile__followers__user=request.user)).order_by("?")
    .annotate(
    mutual_count=Count(
    'poster_profile', filter=Q(poster_profile__profile__following__followers__user=request.user)
    ) # Got the count for mutual followers
    )
    .prefetch_related( # what am I doing wrong here....
    Prefetch(
    'poster_profile',
    Post.objects.annotate(
    is_mutual=Exists(
    Post.objects.filter(
    poster_profile=OuterRef('pk'),
    poster_profile__profile__followers__user=request.user,
    )
    )
    ).filter(is_mutual=True),
    to_attr='mutual_followers',
    )
    )
    )

    # Template

    {% for post in posts %}
    {{ post.mutual_count }} # Got mutual count
    {% endfor %}

    # This is not displaying images.
    {% for post in posts %}
    {% for img in post.mutual_followers %}
    <img src="{{ img.profile_pic.url }}" class="hover-followers-img"/>
    {% endfor %}
    {% endfor %}

    Continue reading...

Compartilhe esta Página