9
class User(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()
    gender = models.IntegerField()
    email = models.CharField(max_length=100)
    password = models.CharField(max_length=255)
    following = models.ManyToManyField("self", related_name='followers')
    objects = UserManager()
    def __repr__(self):
        return "User: {0}".format(self.name)

In my model, User, users can follow and followed by each other.

I can find who the user is following by this:

user1 = User.objects.get(id=1)
following = user1.following.all()

However, I can't figure out how to find whom the user is followed by.

I tried:

user1.followers.all()

since it is the related_name in the following field in my model.

Can anybody please teach me how to do this?

Thank you very much.

Jung
  • 219
  • 3
  • 10
  • Possible duplicate of [How to implement followers/following in Django](https://stackoverflow.com/questions/6218175/how-to-implement-followers-following-in-django) – Jmills Oct 12 '18 at 18:57

1 Answers1

24

You should pass the symmetrical attribute to be False.

From the docs:

When Django processes this model, it identifies that it has a ManyToManyField on itself, and as a result, it doesn’t add a followers attribute to the User class. Instead, the ManyToManyField is assumed to be symmetrical – that is, if I am your follower, then you are my follower.

If you do not want symmetry in many-to-many relationships with self, set symmetrical to False. This will force Django to add the descriptor for the reverse relationship, allowing ManyToManyField relationships to be non-symmetrical.

In your case:

class User(models.Model):
    # ...
    following = models.ManyToManyField('self', related_name='followers', symmetrical=False)
    # ...

Then in your views.py you can access both:

following = user.following.all()
followers = user.followers.all()

Since now the relationship with self is non-symmetrical.

Community
  • 1
  • 1
wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • Any idea on how I avoid following being the same as follower? I mean, how to avoid a User following himself with that approach? Thanks. – Eduardo Gomes Jun 16 '22 at 20:34
  • I managed to achieve something with this code inside `User` model: `def safe_following(self): return self.following.exclude(id=self.pk)` but I'd like to create a field constraint that would avoid self following when saving the data. – Eduardo Gomes Jun 16 '22 at 21:18