Error while accesing manytomany value

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:
and I get:
AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'

This is the UserInteraction

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

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



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


            # Prevents the user from following himself






    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.

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.

Changed the related_name to ‘follows’ and it keeps giving the same error :thinking:

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.

If I set the fields as following:

    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')


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')
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:

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 (?)

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.