简体   繁体   English

Django 全局多对多关系(超过对称)

[英]Django global ManyToMany Relation (more than symmetrical)

I have trouble on how to make a ManyToMany relation where every object is connected to every other object.我很难建立一个多对多关系,其中每个 object 都连接到每个其他 object。

Here's my example for better understanding:这是我的示例以便更好地理解:

class Animal(models.Model):
    animaux_lies = models.ManyToManyField("self", verbose_name="Animaux liés", blank=True)

If y have only two animals linked together, it works fine, thez I correctly linked together in both ways (because the relation is symmetrical).如果 y 只有两个动物连接在一起,它工作正常,z 我正确地以两种方式连接在一起(因为关系是对称的)。 But if I have 3 or more animals, I don't get the result I want.但如果我有 3 只或更多动物,我就得不到我想要的结果。 If Animal1 is linked to Animal2 and Animal3, I would like Animal2 to not only be linked to Animal1 but also to Animal3 (and Animal3 linked to 1 and 2).如果 Animal1 链接到 Animal2 和 Animal3,我希望 Animal2 不仅链接到 Animal1 还链接到 Animal3(并且 Animal3 链接到 1 和 2)。 How can I do that?我怎样才能做到这一点? Even with a through table I don't see how to do this correctly即使有一个直通表,我也看不到如何正确执行此操作

It sounds like you're trying to represent graphs and ask questions about connections within those graphs.听起来您正在尝试表示图表并询问有关这些图表中连接的问题。 That is extremely challenging using a relational database, which is what Django's ORM works with.使用关系数据库是非常具有挑战性的,这是 Django 的 ORM 使用的。 It is possible using a graph database, but that will require different libraries to query the data.可以使用图形数据库,但这需要不同的库来查询数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM