![](/img/trans.png)
[英]Django Admin Forms with additional fields in ManyToMany relations
[英]ManyToMany Relationship with additional fields Django Forms
對於我的 Django 項目,我正在嘗試創建一個單數形式,允許我在我的管道 model 中添加一行,並通過 PipelineProcess model 將該行鏈接到流程 model。如果 PipelineProcess model 沒有,則使用 ModelForms 非常簡單一個額外的領域(階段)。
模型.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')
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)
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 dev 相當陌生。 任何幫助將不勝感激。
嘗試進行以下調整:
forms.py
class PipelineForm(ModelForm):
phase = forms.Charfield()
class Meta:
model = Pipeline
fields = ['name', 'sector']
我不必在fields
列表中包含“phase”,它就可以工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.