0

I have a model Profile (user's profile) and it has many to many relation to self named followings. This manages followers / followings system like one in twitter.

class Profile(models.Model):
    followings = models.ManyToManyField('self', blank = True, related_name = 'followers')

I can get followings just get followings of an User but how to query followers of a user using django model query?

Bigair
  • 1,452
  • 3
  • 15
  • 42
  • I've never been able to make sense of many to many fields. sorry. – Peter Nielsen Apr 06 '21 at 23:46
  • @Bigair I would recommend you to use this plugin instead https://github.com/revsys/django-friendship – binpy Apr 06 '21 at 23:53
  • simply use the related name `followers`, e.g. assume you have a `p = Profile.objects.last()`, then `p.followers.all()` returns a queryset of all related objects to the object `p` – Ersain Apr 07 '21 at 03:30
  • I got 'Profile' object has no attribute 'followers' error – Bigair Apr 07 '21 at 11:57
  • Check the following article: https://stackoverflow.com/questions/45847930/django-in-many-to-many-relationship-within-the-self-class-how-do-i-reference-e – Zhivko Zaikov Apr 07 '21 at 13:58

0 Answers0