我是django的新手,我正在网站上生成工程变更文档以支持产品设计活动。我有三个带有嵌套“多对多”字段的模型。开始生成文档时,我要求用户选择要为其生成文档的机箱,一旦选择了机箱,我想显示一个表格,其中包含“ DocDetials”模型中的所有字段以及一个表格,其中显示了所有可用于选中的机箱作为复选框,以便用户可以选择将更改应用到的主板。
我可以使用视图在模板中显示大多数DocDetails字段,并在峰会之后获取数据。我只需要能够显示和选择相应机箱的板即可。知道用户选择了哪个“机箱”后,假设发送到视图的表单具有默认的“机箱”。
几天来,我一直在寻找一种实现此目的的方法,尝试了几种方法,但一直未能做到这一点。关于如何显示一层多对多文件的答案很多,但是与 NESTED 多对多字段无关。我在SO尚未找到相关的解决方案。希望有人能指出我正确的方向。
这是models.py
class Board(models.Model):
# Board in chassis#
name = models.CharField(max_length = 20)
SMT = models.CharField(max_length = 20)
HM = models.CharField(max_length = 20)
def __str__(self):
#string representing the model object#
return self.name
class Chassis(models.Model):
#chassis information#
name = models.CharField(primary_key=True, max_length = 10)
board = models.ManyToManyField(Board)
fa = models.CharField(max_length = 20, default= "")
def __str__(self):
#string representing the model object#
return self.name
class DocDetials(models.Model):
#Document Details#
chassis = models.ManyToManyField(Chassis,blank = True)
title = models.CharField(max_length = 50, help_text='Enter title for this document')
content = models.TextField(max_length = 100, help_text='Enter detail explanation of the actions to be taken by this document')
reason1 = models.TextField(max_length = 100, help_text='Explain the reason of the above mentioned changes')
effect = models.TextField(max_length = 100, help_text='Explain what effct will the above mentioned change will have on the product')
side_effect = models.TextField(max_length = 100, help_text='Mention any known side effect that could occur after applying these changes')
other_info = models.TextField(max_length = 100, help_text='Mention other information related to these changes')
number = models.IntegerField(null=True, blank=True)
date = models.DateField(default=datetime.date.today, blank=True )
plannedby = models.TextField(max_length = 100)
checkedby1 = models.TextField(max_length = 100)
checkedby2 = models.TextField(max_length = 100)
approvedby = models.TextField(max_length = 100)
def __str__(self):
return self.title
这是forms.py
class DocForm(forms.ModelForm):
class Meta():
model = DocDetials
widgets = {
'title': forms.TextInput(attrs={'class':'textinputclass'}),
'content': forms.Textarea(attrs={'rows':6, 'cols':200}),
'reason1': forms.Textarea(attrs={'rows':6, 'cols':200}),
'effect': forms.Textarea(attrs={'rows':6, 'cols':200}),
'side_effect': forms.Textarea(attrs={'rows':6, 'cols':200}),
'other_info': forms.Textarea(attrs={'rows':6, 'cols':200}),
}
fields = '__all__'