Many to many field django add the relationship both way

后端 未结 2 689
半阙折子戏
半阙折子戏 2021-01-22 16:15

I\'m trying to do a function that allow a user to follow another one. the problem is when I\'m adding a new user to the \"followings\" the user that follow another user is also

相关标签:
2条回答
  • 2021-01-22 16:50

    By default, many-to-many relationships on self are symmetrical. If you don't want this, set symmetrical to False:

    followings = models.ManyToManyField('self', blank=True, symmetrical=False)
    

    See the docs

    0 讨论(0)
  • 2021-01-22 17:03

    Just set related_name="followed_by" for the many to many field. That would assign the reverse mapping to followed_by

    followings = models.ManyToManyField('self', blank=True, symmetrical=False, related_name='followed_by')
    
    0 讨论(0)
提交回复
热议问题