我正在尝试构建一个可以提供报告跟踪的表单。报告本身是ProjectReport
类,用于链接所有可用ReportAtoms
的实例。每个ReportAtom
都是RAGItem
的组合(例如"进度","风险","资源")和RAGMarker
(" Green"," Red"):
class ProjectStatus(CoreBase):
report_project = models.ForeignKey('Project')
report_items = models.ManyToManyField('RAGReportAtom')
report_date = models.DateTimeField(default=datetime.now())
class RAGReportAtom(CoreBase):
item = models.ForeignKey(DicRAGItem)
marker = models.ForeignKey(DicRAGMarker)
comments = models.TextField(null=True, blank=True)
class DicRAGItem(DicCoreBase):
name = models.CharField(max_length=64, unique=True)
description = models.TextField(null=True, blank=True)
is_used_for_portfolio = models.BooleanField(default=False)
is_used_for_project = models.BooleanField(default=True)
is_used_for_milestone = models.BooleanField(default=True
class DicRAGMarker(DicCoreBase):
name = models.CharField(max_length=16, unique=True)
color_code = models.CharField(max_length=7, unique=True)
icon = models.FileField(upload_to='RAG_SIMPLE_ICONS', null=True, blank=True)
这是如何以图形方式表示的:
所以我在这里尝试的基本上是这样的:
类似的东西:
这是否可以用Django.forms和crispy生成这个表单?我正在为大多数表单使用django rest框架 - 也许我也可以使用它?
答案 0 :(得分:1)
你实际上甚至不需要香脆的形式来做到这一点。
Formsets是要走的路! Official documentation
基本上它看起来像:
from django.forms.formsets import formset_factory
from myapp.models import ProjectReport
from myapp.forms import ReportAtomForm
# We use values() because initial data needs to be a dict
list_of_reports = ProjectReport.objects.get(id=key).report_items.values()
ReportAtomFormSet = formset_factory(ReportAtomForm)
formset = ArticleFormSet(initial=list_of_reports)
然后您的视图需要处理文档中描述的formset。对于标记和项目,它们必须是ReportAtomForm中的内联表单集,如您所见:https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#inline-formsets