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
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
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.