Django - 如何在 Django 中建立与多个 ManyToManyField 模型相关的关系

问题描述 投票:0回答:0

我是django初学者。

我这里有两个模型在另一个模型中用作 ManyToManyField:

models.py中:

class Likelihoods(models.Model):
Likelihood = models.CharField(primary_key=True, max_length=255)
Param = models.CharField(max_length=255)

class Consequences(models.Model):
Consequence = models.CharField(primary_key=True, max_length=255)
Param = models.CharField(max_length=255)

class Inherent_risks(models.Model):
    Likelihood = models.ManyToManyField(Likelihoods, related_name= 'InherentRisk_Likelihood')
    Consequence = models.ManyToManyField(Consequences, related_name= 'InherentRisk_Consequence')

在此之后,我将它添加到另一个模型以创建一个表单名称

Hira
.

class Hira(models.Model):

    Activity = models.CharField(max_length=255)
    Risk_Description = models.TextField(default="***")
    PBS = models.ManyToManyField(PBS)
    Physical_control = models.CharField(max_length=255)
    Administrative_control = models.CharField(max_length=255)
    Instrumented_control = models.CharField(max_length=255)
    Inherent_Risk = models.ForeignKey(Inherent_risks, on_delete=models.CASCADE, related_name='Inherent_risk' )

form.py

class HiraForm(ModelForm):
    class Meta:
        model = Hira
        fields = '__all__'

问题: The form i am getting 我不能同时选择

likelihood 
consequence 
(甚至没有任何数据)。 我做错了什么?

python django django-models foreign-keys many-to-many
© www.soinside.com 2019 - 2024. All rights reserved.