我正在使用 cms Plone 构建一个包含其他两个模式的表单。
使用Group Forms,我已经能够包含两个模式的两个字段。但是,它们会丢失所有属性,例如隐藏或当我使用 datagridfield 构建表时。我想要做的是让这两个表单都带有它们的字段,并且在保存时能够将它们保存在单击链接的对象中作为父对象->对象1 [表单顶部]->对象2 [表格底部]
这是我的python代码:
class QuestionPart(group.Group):
label = u'Question Part'
fields = field.Fields(IQuestionPart)
template = ViewPageTemplateFile('questionpart_templates/view.pt')
class Question(group.Group):
label = u'Question'
fields = field.Fields(IQuestion)
template = ViewPageTemplateFile('question_templates/view.pt')
class QuestionSinglePart(group.GroupForm, form.AddForm):
grok.name('register')
grok.require('zope2.View')
grok.context(ISiteRoot)
label = u"Question with Single Part"
ignoreContext = True
enable_form_tabbing = False
groups = (Question,QuestionPart)
def update(self):
super(QuestionSinglePart, self).update()
此代码显示 IQuestion、IQuestionPart 的两个字段,而不考虑以下内容:form.mode(contype='hidden') 或 DataGridField 小部件。我找到了一种显示带有字段提示的正确表单的方法。
class QuestionSinglePart(AutoExtensibleForm, form.AddForm):
grok.require('zope2.View')
grok.context(ISiteRoot)
label = u"Question"
schema = IQuestion
additionalSchemata = (IQuestionPart,)
我觉得我还有很长的路要走。我和一些人谈过。我现在正在尝试使用单独的表单和视图。
到目前为止,我的代码在这一点上:
class QuestionSinglePartForm(AutoExtensibleForm, form.Form):
ignoreContext = True
autoGroups = True
template = ViewPageTemplateFile('questionsinglepart_templates/questionsinglepartform.pt')
@property
def additionalSchemata(self):
return self._additionalSchemata
def __init__(self, context, request, schema, additional=()):
self.context = context
self.request = request
if not IInterface.providedBy(schema):
raise ValueError('Schema is not interface object')
self._schema = schema
if not all(IInterface.providedBy(s) for s in additional):
raise ValueError('Additional schema is not interface')
self._additionalSchemata = additional
class QuestionSinglePartView(object):
schema = IQuestion
additional = (IQuestionPart,)
def __init__(self, context, request):
self.context = context
self.request = request
self.form = QuestionSinglePartForm(context, request, self.schema, self.additional)
def magic(self, data, errors):
pass
"""
question = Question()
question.number = data['number']
question.questionContent = data['questionContent']
questionPart = QuestionPart()
questionPart.typeOfQuestion = data['IQuestionPart.typeOfQuestion']
questionPart.explanation = data['IQuestionPart.explanation']
questionPart.fileSize = data['IQuestionPart.fileSize']
questionPart.fileType = data['IQuestionPart.fileType']
questionPart.hints = data['IQuestionPart.hints']
questionPart.table = data['IQuestionPart.table']
questionPart.contype = data['IQuestionPart.contype']
questionPart.content = data['IQuestionPart.content']
"""
def update(self, *args, **kwargs):
if self.request.get('REQUEST_METHOD') == 'POST':
data, errors = self.form.extractData()
self.magic(data, errors)
self.formdisplay = self.form.render()
def __call__(self, *args, **kwargs):
self.update(*args, **kwargs)
return self.index(*args, **kwargs)
我正在为表单的呈现而苦苦挣扎,并且 QuestionSinglePart 对象没有索引属性。
在与一些 plone 开发人员一起工作了几个小时后,我们已经弄清楚发生了什么。
我遗漏了:
@property
def schema(self):
return self._schema
我需要在视图中定义一个索引,如下所示:
index = ViewPageTemplateFile('questionsinglepart_templates/questionsinglepart.pt')
我需要将此添加到视图init中:
alsoProvides(self.form, IWrappedForm)
在视图的更新方法中,我需要在表单显示之前调用它。我还可以删除数据提取并将其移至表单。
def update(self, *args, **kwargs):
self.form.update(*args, **kwargs)
self.formdisplay = self.form.render()
我目前仍在努力将数据保存到对象中。