是否可以使用一个视图创建多个表单

时间:2017-05-22 21:10:48

标签: django forms

我想为我的模型的每个属性创建一个评论表单。通常,我会为每个模型属性提供一个视图和一个表单。问题是,对于30个属性,我不想创建30个表单和30个视图。有没有办法为所有属性创建一个视图和一个表单,然后为每个属性都有一个注释表单?

我的模特是:

class Project(models.Model):

   attribut1 = models.CharField(choices=PROJECT_STATUS,default=1)
   attribut2 = models.CharField(choices=PROJECT_STATUS,default=1)
   attribut3 = models.CharField(choices=PROJECT_STATUS,default=1)
   attribut4 = models.CharField(choices=PROJECT_STATUS,default=1)
   attribut5 = models.CharField(choices=PROJECT_STATUS,default=1)
   attribut6 = models.CharField(choices=PROJECT_STATUS,default=1)
   attribut7 = models.CharField(choices=PROJECT_STATUS,default=1)

我的表格是:

class ScopeForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super(ScopeForm, self).__init__(*args, **kwargs)
    for field in self.fields:
        self.fields[field].required = False

class Meta:
    model = Project
    fields = ['project_scope',]

0 个答案:

没有答案