在Dexterity中使用collective.z3cform.datagridfield
我对Plone还不是很熟悉,现在在尝试用DataGridField和Dexterity一起使用。我的目标是利用Plone 4.1在我们的内部网发布一项可用性研究的结果。我创建了一个自定义文档类型(叫做Interaction),想用数据表格来展示一个包含两列的表格,显示研究结果的总结。
根据collective.z3cform.datagridfield上的说明,我已经成功地把collective.z3cform.datagrid这个包添加到我的构建列表中,现在在我的网站的插件列表中可以看到这个新插件是激活状态。我还创建了一个简单的Python模块,描述了一个文档,记录了我正在整理的可用性研究的发现:
from five import grok
from zope import schema
from zope import interface
from plone.directives import form
from plonetheme.mytheme import InteractionMessageFactory as _
from plone.app.textfield import RichText
from z3c.form import field, button
from Products.CMFCore.interfaces import IFolderish
from collective.z3cform.datagridfield import DataGridFieldFactory, DictRow
class IFinding(interface.Interface):
summary = schema.TextLine(title=_(u"Summary"))
percentage = schema.TextLine(title=_(u"Percentage"))
class IInteraction(form.Schema):
findings = schema.List(
title=_(u"Overview of findings"),
required=False,
value_type=DictRow(
title=_(u"Finding"),
schema=IFinding
)
)
class EditForm(form.EditForm):
grok.context(IInteraction)
grok.require('zope2.View')
fields = field.Fields(IInteraction)
fields['findings'].widgetFactory = DataGridFieldFactory
我通过在profiles/default/types.xml文件中添加一行,注册了我的新Interaction内容类型:
<?xml version="1.0"?>
<object meta_type="Plone Types Tool" name="portal_types">
<property name="title">Controls the available content types in your portal</property>
<object meta_type="Dexterity FTI" name="interaction" />
<!-- -*- extra stuff goes here -*- -->
</object>
为了完整性,我还包括了相应的profiles/default/types/interaction.xml文件:
<?xml version="1.0"?>
<object name="interaction" meta_type="Dexterity FTI"
xmlns:i18n="http://xml.zope.org/namespaces/i18n">
<property name="title">Interaction</property>
<property name="description">An item in the interactions dictionary</property>
<property name="icon_expr">string:${portal_url}/document_icon.png</property>
<property name="factory">interaction</property>
<property name="link_target"></property>
<property name="immediate_view">view</property>
<property name="global_allow">True</property>
<property name="filter_content_types">True</property>
<property name="allowed_content_types"/>
<property name="allow_discussion">False</property>
<property name="default_view">view</property>
<property name="view_methods">
<element value="view"/>
</property>
<property name="default_view_fallback">False</property>
<property name="add_permission">cmf.AddPortalContent</property>
<property name="klass">plone.dexterity.content.Item</property>
<property name="behaviors">
<element value="plone.app.dexterity.behaviors.metadata.IDublinCore"/>
<element value="plone.app.content.interfaces.INameFromTitle"/>
<element value="collective.flowplayer.behaviors.IFlowplayerFile"/>
</property>
<property name="schema">plonetheme.mytheme.interaction.IInteraction</property>
<property name="model_file"></property>
<alias from="(Default)" to="(dynamic view)"/>
<alias from="edit" to="@@edit"/>
<alias from="sharing" to="@@sharing"/>
<alias from="view" to="(selected layout)"/>
<action title="View" action_id="view" category="object" condition_expr=""
icon_expr="" link_target="" url_expr="string:${object_url}"
visible="True">
<permission value="View"/>
</action>
<action title="Edit" action_id="edit" category="object" condition_expr=""
icon_expr="" link_target="" url_expr="string:${object_url}/edit"
visible="True">
<permission value="Modify portal content"/>
</action>
</object>
当我去添加我的Interaction自定义类型的表单时,看到的却是一个标准的Dexterity列表项添加/删除小部件,而不是在collective.z3cform.datagrid_demo示例中看到的数据表格小部件。当我尝试保存这个自定义类型时,Dexterity列表小部件显示了一个验证错误,提示“系统无法处理给定的值。”
我还需要添加其他代码吗?我需要覆盖Dexterity的添加/编辑表单视图模板吗?
3 个回答
1
在Plone 5.0.4中,这对我有效。
from zope import schema
from zope.interface import Interface
from plone.supermodel import model
from plone.autoform import directives
from collective.z3cform.datagridfield import DataGridFieldFactory, DictRow
from plonetheme.mytheme import InteractionMessageFactory as _
class IFindingRow(Interface):
summary = schema.TextLine(title=_(u'Summary'),
required=False)
percentage = schema.TextLine(title=_(u'Percentage'),
required=False)
class IInteraction(model.Schema):
directives.widget(findings=DataGridFieldFactory)
findings= schema.List(
title=_(u"Overview of findings"),
value_type=DictRow(title=_(u"Finding"),
schema=IFindingRow),
required=False,
)
1
试着使用Dexterity表单提示:
...
from zope import schema
from zope.interface import Interface
from plone.directives import form
from collective.z3cform.datagridfield import DataGridFieldFactory, DictRow
from plonetheme.mytheme import InteractionMessageFactory as _
...
class IFindingRow(Interface):
summary = schema.TextLine(title=_(u'Summary'),
required=False)
percentage = schema.TextLine(title=_(u'Percentage'),
required=False)
class IInteraction(form.Schema):
...
form.widget(findings=DataGridFieldFactory)
findings= schema.List(
title=_(u"Overview of findings"),
value_type=DictRow(title=_(u"Finding"),
schema=IFindingRow),
required=False,
)
2
你按照说明书上的步骤在操作,但就是不行。这是一个已知的问题: