Django: I get: Cannot query "admin": Must be "Follow" instance in my code when trying to implement unfollow button

I'm a beginner and I'm trying to create a small network project in which users can follow each other. I have implemented the follow button right, so it updates my models and displays proper info to users, but I can't get unfollow to work properly. I'm guessing it's something to do with the way I implemented follow model (with many to many field), but I'd like to implement it this way for practice... Anyhow, here's the code:

Models:

class User(AbstractUser):
    pass
class Follow(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_follow")
    following = models.ManyToManyField(User, blank=True, related_name="followers")

And view:

def users(request, username):
    """Displaying user profiles"""

    if request.method == "POST":
            user = request.user
            profile = User.objects.get(username=username)
            follow = Follow(user=user)
            follow.save()
            if "unfollow" in request.POST:
                profile.followers.remove(user)
                follow.following.remove(profile)
                return HttpResponseRedirect(reverse('users', args=(username,)))
            elif "follow" in request.POST:
                follow.following.add(profile)
                return HttpResponseRedirect(reverse('users', args=(username,)))

This code yields in: "ValueError at /users/test Cannot query "admin": Must be "Follow" instance." at the profile.followers.remove(user) line...

Playing with it in shell I found out (at least I think so) that the line under it (follow.following.remove(profile) - which by the way was there before I tried with the profile.followers.remove(user)) removes the profile from Follow model, but for some reason it is not by itself updated in the Users model (for followers) ???

    from django.db import models
    
    # Create your models here.
    class User(models.Model):
        name = models.CharField(max_length=40)
        pwd = models.CharField(max_length=40)
    
        def __str__(self):
            return self.name
    
    
    class Follow(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        another_user = models.ManyToManyField(User, related_name='another_user')
    
        def __str__(self):
            return self.user.name
============================================================================
views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import User, Follow
# Create your views here.
def index(request):
    if 'user' in request.session:
        return render(request, 'index.html')
    else:
        return redirect('login')



def profile(request, user_name):
    user_obj = User.objects.get(name=user_name)
    session_user = User.objects.get(name=request.session['user'])
        session_following, create = Followers.objects.get_or_create(user=session_user)
    following, create = Followers.objects.get_or_create(user=session_user.id)
    check_user_followers = Followers.objects.filter(another_user=user_obj)

    is_followed = False
    if session_following.another_user.filter(name=user_name).exists() or following.another_user.filter(name=user_name).exists():
        is_followed=True
    else:
        is_followed=False
    param = {'user_obj': user_obj,'followers':check_user_followers, 'following': following,'is_followed':is_followed}
    if 'user' in request.session:
        return render(request, 'profile.html', param)
    else:
        return redirect('index')


def follow_user(request, user_name):
    other_user = User.objects.get(name=user_name)
    session_user = request.session['user']
    get_user = User.objects.get(name=session_user)
    check_follower = Followers.objects.get(user=get_user.id)
    is_followed = False
    if other_user.name != session_user:
        if check_follower.another_user.filter(name=other_user).exists():
            add_usr = Followers.objects.get(user=get_user)
            add_usr.another_user.remove(other_user)
            is_followed = False
            return redirect(f'/profile/{session_user}')
        else:
            add_usr = Followers.objects.get(user=get_user)
            add_usr.another_user.add(other_user)
            is_followed = True
            return redirect(f'/profile/{session_user}')

        return redirect(f'/profile/{session_user}')
    else:
        return redirect(f'/profile/{session_user}')

============================================================================= User This For Reference...Follow And Unfollw Logic

Back to Top