Django模型自省—许多对许多Vs许多对许多

时间:2021-04-25 09:58:20

I have a django model with 2 many to many relations to the same model.

我有一个django模型,其中有两个与相同模型的许多关系。

One of them, uses the "through" options, like:

其中一个选项使用了“通过”选项,比如:

class MyModel(models.Model):
    ....
    property1 = models.ManyToManyField(Model2, related_name="internal", blank=True, null=True)
    property2 = models.ManyToManyField(
        Model2,
        related_name="external",
        through="Model3"
    )

While iterating over model_instance._meta.m2m_data how can I check for "property2"?

当遍历model_instance._meta。m2m_data如何检查"property2"?

both fields have "rel.through" set.. I was expecting just the second field.. while in the first one I would have "rel.to" but not "rel.through"

两个字段都有“rel.through”设置。我正期待着第二个领域。而在第一个中,我将使用“rel.to”而不是“rel.through”

1 个解决方案

#1


1  

In my own example, when field is propery1, is_hidden() would return True, because there is this concept about hidden relationship. Also, property2.rel.id_hidden() would return False since Model3 represents this relationship.

在我自己的示例中,当字段为propery1时,is_hidden()将返回True,因为有一个关于隐藏关系的概念。另外,property2.rel.id_hidden()将返回False,因为Model3表示此关系。

# for some reason, User.groups has field.is_hidden() == None
if field.rel.is_hidden() == None:
    hidden_field = True
else:
    hidden_field = field.rel.is_hidden()

Usage here: https://github.com/felipecruz/dmqs/blob/master/dmqs/integration/memorify_django_model.py#L28

使用:https://github.com/felipecruz/dmqs/blob/master/dmqs/integration/memorify_django_model.py L28

#1


1  

In my own example, when field is propery1, is_hidden() would return True, because there is this concept about hidden relationship. Also, property2.rel.id_hidden() would return False since Model3 represents this relationship.

在我自己的示例中,当字段为propery1时,is_hidden()将返回True,因为有一个关于隐藏关系的概念。另外,property2.rel.id_hidden()将返回False,因为Model3表示此关系。

# for some reason, User.groups has field.is_hidden() == None
if field.rel.is_hidden() == None:
    hidden_field = True
else:
    hidden_field = field.rel.is_hidden()

Usage here: https://github.com/felipecruz/dmqs/blob/master/dmqs/integration/memorify_django_model.py#L28

使用:https://github.com/felipecruz/dmqs/blob/master/dmqs/integration/memorify_django_model.py L28