且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

递归多对多关系的相关名称不起作用

更新时间:2023-11-17 17:42:16

您需要设置 symmetrical = False 。作为 ManyToManyField的文档说:


当Django处理此模型时,它会识别出其自身具有ManyToManyField,因此不会添加person_set属性到Person类。取而代之的是,ManyToManyField假定是对称的,也就是说,如果我是你的朋友,那么你就是我的朋友。

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

如果您不希望与以下对象的多对多关系对称自我,将对称设置为False。这将迫使Django为反向关系添加描述符,从而允许ManyToManyField关系是非对称的。

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.