如何在Django表单中添加一个through模型的字段



对于我的Django项目,我试图创建一个单一的形式,允许我在我的管道模型中添加一行,并通过PipelineProcess模型将该行链接到Process模型。如果PipelineProcess模型没有额外的字段(阶段),那么使用ModelForms就相当简单了。我需要能够查看和添加/编辑PipelineProcess。相同形式的相场。注意,PipelineProcess是一个"通过"模型。管道和过程模型

models.py

class Pipeline(models.Model):
name = models.CharField(max_length=100)
sector = models.CharField(max_length=100, blank=True, null=True)
process = models.ManyToManyField(Process, through='PipelineProcess') ### NOTE THE USAGE OF THE THROUGH MODEL HERE
class Meta:
managed = True
db_table = 'pipeline'

class Process(models.Model):
name = models.CharField(max_length=100)
class Meta:
managed = True
db_table = 'process'

class PipelineProcess(models.Model):
pipeline = models.ForeignKey(Pipeline, models.DO_NOTHING, blank=True, null=False)
process = models.ForeignKey(Process, models.DO_NOTHING, blank=True, null=False)
phase = models.CharField(max_length=100) ### THIS FIELD I AM UNABLE TO ACCESS THROUGH A FORM
class Meta:
managed = True
db_table = 'pipeline_process'

forms.py

class PipelineForm(ModelForm):
class Meta:
model = Pipeline
fields = ['name', 'sector', 'phase']

这个表单生成了以下有意义的错误:

django.core.exceptions.FieldError: Unknown field(s) (phase) specified for Pipeline

我试着查找其他人的例子,但没有一个对我有用。大多数都太复杂了,我无法把它们放到自己的项目中。我是Django和web开发的新手。如有任何帮助,我将不胜感激。

class PipelineForm(Forms.ModelForm):

class Meta:
model = Pipeline
fields = '__all__'

字段"phase".py不是"Pipeline"的字段。类。还要加上modelforms;而不是"Forms.ModelForm">

相关内容

最新更新