首先,让我解释一下我想要实现的目标。我有一个Django应用程序从第三方源获取它的数据,所以我不能使用默认的Django模型类或模型表单类开始。我现在一直在使用django-crispyforms创建表单。
假设我有一个'Workflow'类,其中包含一组'Workcenters',每个Workcenter都可以拥有它自己的Workunits集。每个Workunit都可以是某种类型......每个不同的Workunit类型都可能有额外的字段。
现在我被要求提供一个表单,用户可以在其中更改有关“工作中心”的一般信息,还可以更改该工作中心中每个“工作单元”的一些数据。由于我事先不知道Workcenter中有多少Workunit,也不知道它将是什么类型的Workunit,因此动态构建此表单非常重要。此外,他们希望每个Workunit详细信息都位于选项卡持有者的选项卡上。
我已经能够生成一个基本表单,其中包含每个工作中心的选项卡,并且在这些选项卡中为工作中心中的每个工作单元生成必要的选项卡,但我现在似乎被卡住了......
现在我的问题是......我应该怎样处理这个问题。我应该为每个WorkunitType创建布局(当然使用继承来继承公共部分),并动态添加它们吗?我应该使用表格和某种子表格吗?
为了完整起见......这就是我到目前为止所做的:
# Django Imports
from django import forms
from django.forms import CharField, Form, DateTimeField, PasswordInput, ChoiceField
from django.utils.translation import ugettext_lazy as _
# Crispy Forms Imports
from crispy_forms import layout
from crispy_forms.helper import FormHelper
from crispy_forms.bootstrap import Tab, TabHolder
from crispy_forms.layout import LayoutObject, Layout
# Common Workflower Imports
from common.forms import ReadOnlyFieldsMixin, SubmitCancelButtonsLayout, SubmitCancelFormActions
from common.xeon import Panel
# Add your forms here
########################################################################
class WorkunitLayout(Layout):
""" Base Workunit Layout ... every Workunit Type will have
it's own implementation of this Layout
"""
workunitID = forms.CharField( label = 'ID' )
wuType = forms.CharField( label = 'Type' )
#----------------------------------------------------------------------
def __init__(self, *args, **kwargs):
if kwargs.has_key( 'workunit' ):
self.workunit = kwargs['workunit']
super(WorkunitLayout, self).__init__(
layout.Fieldset(
self.workunit.ID,
layout.Field( 'workunitID'),
layout.Field( 'wuType' ),
layout.HTML('<p>Wat is dit?</p>'),
),
)
########################################################################
class WorkunitTab(Layout):
#----------------------------------------------------------------------
def __init__(self, *args, **kwargs):
"""Constructor"""
if kwargs.has_key( 'workunit' ):
self.workunit = kwargs['workunit']
super (WorkunitTab, self).__init__(
#WorkunitForm(),
WorkunitLayout( workunit = self.workunit ),
#Tab( self.workunit.ID, WorkunitLayout( workunit = self.workunit ) ),
)
########################################################################
class WorkcentersTab(Layout):
def __init__(self, *args, **kwargs):
#if kwargs.has_key( 'workflow' ):
#self.workflow=initial['workflow']
#if kwargs.has_key('workcenter'):
#self.workcenter=kwargs['workcenter']
#print "WorkcentersTab.workcenter={0}".format(self.workcenter)
super(WorkcentersTab, self).__init__(
TabHolder(
Tab( 'Workunits' )
),
)
########################################################################
class workflowForm(forms.Form):
workflowID = forms.CharField(
label=_('ID'),
max_length=100,
)
workflowType = forms.CharField(
label=_('Type'),
max_length=100,
required=True,
)
def __init__(self, *args, **kwargs):
super (workflowForm, self).__init__(*args, **kwargs)
self.workflow = self.initial['APIObject']
self.workcenters = self.workflow.workcenters
self.helper = FormHelper()
#self.helper.layout = layout.Layout(
myLayout = layout.Layout(
Panel(
_('Productie Workflow'),
layout.Fieldset(
'Configuratie',
layout.Row(
layout.Field('workflowID', wrapper_class='col-sm-6'),
layout.Field('workflowType', wrapper_class='col-sm-6'),
),
),
TabHolder(
Tab('Workcenters' ),
),
colored=True,
),
SubmitCancelButtonsLayout(),
)
if ( self.workcenters <> {} ):
print "workcenters={0}".format(self.workcenters)
for name in self.workcenters:
workcenter = self.workcenters[name]
workcenterLayout = Tab(name, WorkcentersTab(workcenter=workcenter))
print "workcenter[{0}]={1}".format(name, workcenter)
##print "workcenterLayout[{0}]={1}".format(name, workcenterLayout)
if workcenter.workunits <> {} :
for workunitname in workcenter.workunits:
workunit = workcenter.workunits[workunitname]
workunitLayout = Tab(workunitname, WorkunitTab(workunit=workunit))
print " workunit[{0}]={1}".format(workunitname, workunit)
workcenterLayout[0][0].append(workunitLayout)
##print " workunitLayout{0}={1}".format(workunitname, workunit)
##workcenterLayout[1].append(workunitLayout)
myLayout[0][1].append(workcenterLayout)
self.helper.layout = myLayout
事实上......我的标签现在是动态创建的,Fieldsets确实出现在标签中,“Wat is dit?”也有,但田地没有出现。我已经尝试了很多东西......遗憾的是到目前为止没有任何运气。