# Problem with adding objects to manytomany fields

**URL:** <https://forum.djangoproject.com/t/problem-with-adding-objects-to-manytomany-fields/13033>\
**Category:** Using the ORM\
**Created:** [April 6, 2022, 3:22pm UTC](https://forum.djangoproject.com/t/problem-with-adding-objects-to-manytomany-fields/13033 "2022-04-06T15:22:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parsa4I](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/parsa4i/32/8021_2.png) [@Parsa4I](https://forum.djangoproject.com/u/Parsa4I)\
**Post date:** [April 6, 2022, 3:22pm UTC](https://forum.djangoproject.com/t/problem-with-adding-objects-to-manytomany-fields/13033/1 "2022-04-06T15:22:43Z")

</div>

I have a User model:

```python
class User(AbstractUser):
    followers_num = models.IntegerField(default=0)
    followings_num = models.IntegerField(default=0)
    followers = models.ManyToManyField('self', null=True, blank=True)
    followings = models.ManyToManyField('self', null=True, blank=True)

```

And there’s a view for adding/removing user objects from followers/followings:

```python
def follow(request, follow, user):
    if (request.method == 'PUT'):
        following_user = User.objects.get(username=user)
        follower_user = User.objects.get(username=request.user.username)
        if follow:
            # Follow
            following_user.followers.add(follower_user)
            following_user.followers_num += 1

            follower_user.followings.add(following_user)
            follower_user.followings_num += 1
        else:
            # Unfollow
            following_user.followers.remove(follower_user)
            following_user.followers_num -= 1

            follower_user.followings.remove(following_user)
            follower_user.followings_num -= 1
        
        following_user.save()
        follower_user.save()
        return HttpResponse(status=204)

```

I want it to add `follower_user` to the followers of `following_user` and add `following_user` to the followings of `follower_user`. However, instead of doing so, it adds `follower_user` not only to the followers of `following_user`, but it also adds it to the followings of `following_user`. Why does it happen?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 6, 2022, 4:15pm UTC](https://forum.djangoproject.com/t/problem-with-adding-objects-to-manytomany-fields/13033/2 "2022-04-06T16:15:52Z")

</div>

This model structure seems “wrong” to me. Why do you have two different self-related ManyToMany fields?

It’s a bad data design to manually track a number of entries easily determined by a `count` function - I would remove those fields.

A ManyToMany relationship is a two-way relationship. By general definition, if Person A follows Person B, then you can just as validly say that Person B is followed by Person A. You don’t need a separate relationship between the two.

Also, it makes no sense to provide for a null or blank value in a ManyToMany field.

I would suggest you fix your model before worrying about the views that need to work with it.
