我正在使用 cms Plone 构建一个包含另外两个模式的表单。
随着Group Forms ,我已经能够包含两个模式的两个字段。但是,当我使用 datagridfield 构建表时,它们会丢失所有属性,例如 hidden 或。我想要做的是让这两种形式都有它们的字段,并且在保存时能够将它们保存在链接被点击为父对象的对象中 -> 对象 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,)
我觉得我还有很长的路要走。我和一些人谈过。我现在正在尝试使用单独的表单和 View 。
到目前为止,我的代码在这一点上:
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
我需要像这样在 View 中定义一个索引:
index = ViewPageTemplateFile('questionsinglepart_templates/questionsinglepart.pt')
我需要将其添加到 View init:
alsoProvides(self.form, IWrappedForm)
在 View 的更新方法中,我需要在表单显示之前调用它。我还可以删除数据提取并将其移动到表单中。
def update(self, *args, **kwargs):
self.form.update(*args, **kwargs)
self.formdisplay = self.form.render()
我目前仍在努力将数据保存到对象中。
最佳答案
只有包含 plone.autoform.base.AutoExtensibleForm
mixin 的表单类才会注意模式表单提示。尝试将其用作您的组表单类的混合(并提供此混合寻找的 schema
属性,而不是 fields
):
from plone.autoform.base import AutoExtensibleForm
class QuestionPart(AutoExtensibleForm, group.Group):
label = u'Question Part'
schema = IQuestionPart
template = ViewPageTemplateFile('questionpart_templates/view.pt')
关于python - 如何创建具有多个模式的 z3c.form?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326572/