我正在尝试在Plone网站上创建一个控制面板插件,用于编辑作为字典类型的注册表记录。
我的目的是存储"类型的供应商"作为注册表中的字典。
我在profiles / default中的registry.xml:
<registry>
<record interface="gpcl.assets.suppliertypes.ISupplierTypes" field="supplier_types">
<value>
<element key="1">Distributor</element>
<element key="2">Manufacturer</element>
<element key="3">Service Provider</element>
</value>
</record>
</registry>
我的界面和表格:
class ISupplierTypes(form.Schema):
""" Define settings data structure
"""
supplier_types = schema.Dict(title=u"Types of Suppliers",
key_type=schema.Int(title=u"supplier_type_id"),
value_type=schema.TextLine(title=u"supplier_type_name",
required=False),
required=False,
)
class SupplierTypesEditForm(RegistryEditForm):
"""
Define form logic
"""
schema = ISupplierTypes
label = u"Types of Suppliers"
description = u"Please enter types of suppliers"
class SupplierTypesView(grok.View):
"""
View class
"""
grok.name("supplier-types")
grok.context(ISiteRoot)
def render(self):
view_factor = layout.wrap_form(SupplierTypesEditForm, ControlPanelFormWrapper)
view = view_factor(self.context, self.request)
return view()
我将它添加到我的profiles / default中的controlpanels.xml和portal_quickinstaller中,我安装了产品,控制面板确实显示在附加组件中并显示显示默认值的字段。 不幸的是,当我尝试添加,编辑或删除时,会显示一条错误消息,指出&#34;包含错误的类型。&#34;我认为我在创建控制面板方面的错误。
为dict类型的记录创建控制面板的正确方法是什么?
具有讽刺意味的是,在视图类的render方法中,我试图看看是否可以打印记录(在此处找到了如何执行此操作:https://pypi.python.org/pypi/plone.app.registry#using-the-records-proxy)并且我能够显示为dict对象。我还能够以编程方式添加一个新的&#34;元素&#34;也记录在案。
就我想要使用dict类型而言,我确实计划利用键值,这就是我想使用字典类型的原因。
如果我使用不正确的术语,我道歉。
提前谢谢。
编辑:
我正在使用Plone 4.3.2。
编辑:
抱歉,我错了。我找到了追溯。2014-08-20 13:13:07 ERROR Zope.SiteErrorLog 1408554787.930.279058908095
http://localhost:8080/GPCLAssetTracker/@@supplier-types/@@z3cform_validate_field
Traceback (innermost last):
Module ZPublisher.Publish, line 138, in publish
Module ZPublisher.mapply, line 72, in mapply
Module ZPublisher.Publish, line 53, in missing_name
Module ZPublisher.HTTPResponse, line 741, in badRequestError
BadRequest: <h2>Site Error</h2>
<p>An error was encountered while publishing this resource.
</p>
<p><strong>Invalid request</strong></p>
The parameter, <em>fname</em>, was omitted from the request.<p>Make sure to specify all required parameters, and try the request again.</p>
<hr noshade="noshade"/>
<p>Troubleshooting Suggestions</p>
<ul>
<li>The URL may be incorrect.</li>
<li>The parameters passed to this resource may be incorrect.</li>
<li>A resource that this resource relies on may be
encountering an error.</li>
</ul>
<p>For more detailed information about the error, please
refer to the error log.
</p>
<p>If the error persists please contact the site maintainer.
Thank you for your patience.
</p>
我也忘了提及,我使用的操作系统是Xubuntu。
答案 0 :(得分:6)
您可以选择不使用schema.Dict
,但有时会使用不同的方法。请记住,这将是危险的:您必须为您的插件提供卸载步骤,否则如果您稍后删除该插件,您的注册表将会被破坏。
这两篇文章对此进行了描述:
用两个词来表达:在schema.Tuple
内使用schema.Object
。为了保持使密钥唯一的dict行为,您必须提供自己的验证器。
另一个好的方法(neved used)是文章中描述的那个&#34;复杂Plone注册表设置重新访问&#34;,但它似乎从网上消失了(......非常悲伤......)。
答案 1 :(得分:4)
另一种我发现非常聪明和简单的方法是collective.cron
正在使用的方法: JSON 。
不必创建具有Object类型的注册表并且不得不关心卸载麻烦等等,只需创建一个文本行字段并在其上转储/加载JSON。
请参阅interface registration或懒人:
class IRegistryCrontab(Interface):
"""Plone.app.registry backend for the crontab persistence"""
activated = schema.Bool(
title=_('Global crontab activation switch'),
default=True
)
crontab = schema.List(
title=_(u'Crons'),
value_type=schema.Text(title=_(u'JSON repr of a cron')),
)
简单,聪明,有效!