Home > database >  Error accesing reverse manytomany field from user to onetoone related model
Error accesing reverse manytomany field from user to onetoone related model

Time:03-01

Im having trouble with a model classs which has a onetoone relation with the User model, this the conflictive part:

class UserInteraction(models.Model):
    user = models.OneToOneField(User, related_name="interactions", on_delete=models.CASCADE)

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

When I try to get the follows/likes by doing user.interactions.user_follows or user.interactions.user_likes I get the following error:

In [18]: u = User.objects.get(id=1)

In [19]: u.interactions.users_likes.all()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [19], in <module>
----> 1 u.interactions.users_likes.all()

File ~\...\django\db\models\fields\related_descriptors.py:536, in ReverseManyToOneDescriptor.__get__(self, instance, cls)
    533 if instance is None:
    534     return self
--> 536 return self.related_manager_cls(instance)

File ~\...\django\db\models\fields\related_descriptors.py:826, in create_forward_many_to_many_manager.<locals>.ManyRelatedManager.__init__(self, instance)
    824     self.prefetch_cache_name = rel.field.name
    825     self.source_field_name = rel.field.m2m_field_name()
--> 826     self.target_field_name = rel.field.m2m_reverse_field_name()
    827     self.symmetrical = rel.symmetrical
    828 else:

File ~\...\django\db\models\fields\related.py:1598, in ManyToManyField._get_m2m_reverse_attr(self, related, attr)
   1596             setattr(self, cache_attr, getattr(f, attr))
   1597             break
-> 1598 return getattr(self, cache_attr)

AttributeError: 'ManyToManyField' object has no attribute '_m2m_reverse_name_cache'

Here is the follower and like models used for through in the fk:

class UserFollow(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 = [
            models.UniqueConstraint(
                name="%(app_label)s_%(class)s_unique_relationships",
                fields=["followed", "follower"],
            ),
            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}'


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

    class Meta:
        constraints = [
            models.UniqueConstraint(
                name="%(app_label)s_%(class)s_unique_relationships",
                fields=["liked", "liker"],
            ),
            models.CheckConstraint(
                name="%(app_label)s_%(class)s_prevent_self_follow",
                check=~models.Q(liker=models.F("liked")),
            ),
        ]

    def __str__(self):
        return f'{self.liker} likes {self.liked}'

In the admin everything works fine, what im doing wrong?

CodePudding user response:

The ForeignKey fields in through models need to reference the model that contains the ManyToManyField and not the model in the OneToOneField - UserInteraction and not User

class UserFollow(TimeStampBase):
    follower = models.ForeignKey(UserInteraction, on_delete=models.CASCADE, related_name='follower')
    followed = models.ForeignKey(UserInteraction, on_delete=models.CASCADE, related_name='followed')
    ...

class UserLike(TimeStampBase):
    liker = models.ForeignKey(UserInteraction, on_delete=models.CASCADE, related_name='liker')
    liked = models.ForeignKey(UserInteraction, on_delete=models.CASCADE, related_name='liked')
    ...
  • Related