# Error while accesing manytomany value

**URL:** <https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349>\
**Category:** Using the ORM\
**Created:** [February 25, 2022, 10:14pm UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349 "2022-02-25T22:14:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![whosgriffith](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/whosgriffith/32/7501_2.png) [@whosgriffith](https://forum.djangoproject.com/u/whosgriffith)\
**Post date:** [February 25, 2022, 10:14pm UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349/1 "2022-02-25T22:14:34Z")

</div>

I have a User model which inherits from AbstractUser, this user will have interactions with a lot of others models, like posts, other users and more (and it doesnt make sense for me to put all of this fields on each model)… because of this I decided to separate that fields from the User model, and created a model UserInteraction with a onetoone relation to User  
An error I have with this is when I try to do:  
`u.interactions.user_follows.all()`  
and I get:  
`AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'`

This is the UserInteraction

```auto
class UserInteraction(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='interactions')

    # User - User

    users_follows = models.ManyToManyField('self', through='Follow', blank=True, symmetrical=False, related_name='user_follows')
    users_likes = models.ManyToManyField('self', through='Like', blank=True, symmetrical=False, related_name='user_likes')

    # User - Posts

    posts_likes = models.ManyToManyField(Post, blank=True, related_name='post_likes')

    posts_dislikes = models.ManyToManyField(Post, blank=True, related_name='post_dislikes')

    posts_favorites = models.ManyToManyField(Post, blank=True, related_name='post_favorites')

    # User - Person

    people_follows = models.ManyToManyField(Person, blank=True, related_name='person_follows')

    people_likes = models.ManyToManyField(Person, blank=True, related_name='person_likes')

    people_favorites = models.ManyToManyField(Person, blank=True, related_name='person_favorites')

```

Follow model

```auto
class Follow(TimeStampBase):

    follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='follower')

    followed = models.ForeignKey(User, on_delete=models.CASCADE, related_name='followed')

    class Meta:

        constraints = [

            # Prevents more than one follow from one user to another

            models.UniqueConstraint(

                name="%(app_label)s_%(class)s_unique_relationships",

                fields=["followed", "follower"],

            ),

            # Prevents the user from following himself

            models.CheckConstraint(

                name="%(app_label)s_%(class)s_prevent_self_follow",

                check=~models.Q(follower=models.F("followed")),

            ),

        ]

    def __str__ (self):

        return f'{self.follower} follows {self.followed}'

```

Is a UserInteraction model the right thing to do? How do I fix that error, couldnt find what Im looking for.

---

<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:** [February 25, 2022, 10:58pm UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349/2 "2022-02-25T22:58:54Z")

</div>

I think the problem you have here is having the related\_name the same as the field name.

From the perspective of a UserInteraction, `user_follows` (the field) is a different set than `user_follows` (the reverse reference). I believe having the same name is causing a field-resolution conflict within Django.

---

<div class="post-metadata">

**Author:** ![whosgriffith](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/whosgriffith/32/7501_2.png) [@whosgriffith](https://forum.djangoproject.com/u/whosgriffith)\
**Post date:** [February 26, 2022, 4:36am UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349/3 "2022-02-26T04:36:51Z")

</div>

Changed the related\_name to ‘follows’ and it keeps giving the same error 🤔

---

<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:** [February 26, 2022, 5:09am UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349/4 "2022-02-26T05:09:18Z")

</div>

The other thing I noticed is that you’re defining a ManyToMany field to UserInteraction, but your through table doesn’t have ForeignKeys to UserInteraction.

---

<div class="post-metadata">

**Author:** ![whosgriffith](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/whosgriffith/32/7501_2.png) [@whosgriffith](https://forum.djangoproject.com/u/whosgriffith)\
**Post date:** [February 26, 2022, 8:45pm UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349/5 "2022-02-26T20:45:17Z")

</div>

If I set the fields as following:

```auto
    users_follows = models.ManyToManyField(User, through='UserFollow', through_fields=('follower', 'followed'), blank=True, symmetrical=False, related_name='user_follows')
    users_likes = models.ManyToManyField(User, through='UserLike', through_fields=('liker', 'liked'), blank=True, symmetrical=False, related_name='user_likes')

```

And:

```auto
class UserFollow(TimeStampBase):

    follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_follower')
    followed = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_followed')

```

```auto
class UserLike(TimeStampBase):

    liker = models.ForeignKey(User, on_delete=models.CASCADE, related_name='liker')
    liked = models.ForeignKey(User, on_delete=models.CASCADE, related_name='liked')

```

I get the following error when doing makemigrations:

```auto
users.UserFollow: (fields.E336) The model is used as an intermediate model by 'users.UserInteraction.users_follows', but it does not have a foreign key to 'UserInteraction' or 'User'.
users.UserInteraction.users_follows: (fields.E339) 'UserFollow.follower' is not a foreign key to 'UserInteraction'.
users.UserInteraction.users_likes: (fields.E339) 'UserLike.liker' is not a foreign key to 'UserInteraction'.
users.UserLike: (fields.E336) The model is used as an intermediate model by 'users.UserInteraction.users_likes', but it does not have a foreign key to 'UserInteraction' or 'User'.

```

And if I change the UserFollow and UserLike foreignkeys to point to UserInteraction as it says in the error, the error changes saying it should be a foreignkey to User (?)

---

<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:** [February 26, 2022, 11:06pm UTC](https://forum.djangoproject.com/t/error-while-accesing-manytomany-value/12349/6 "2022-02-26T23:06:50Z")

</div>

Right basic idea, but you went in the wrong direction.

For the purposes of these ManyToMany relationships, the OneToOne relationship between UserInteraction and User is irrelevant.  
**All** components involved in these relationships are to, with, and for the UserInteraction model. There should be **no** reference to User among _any_ of these tables, other than the OneToOn with User in UserInteraction itself.
