plone - 如何将注释与 z3c.form 的 DictionaryField 一起使用

标签 plone zope z3c.form

documentation关于使用 Python dict 与 z3c.form(加载和存储表单数据)。

但是,用于字典的 z3c.form datamanager 未注册其他类型或接口(interface)(请参阅 reference ),而注释通常使用类似 持久字典

在这种情况下如何使用DictionaryField datamanager? IE。这样,在表单的 getContent 方法中,我仅返回 PersistentDict 注释。

最佳答案

不幸的是,对于这个要求似乎没有简单的解决方案。 我曾经在 z3c 表单中使用 datagrid 字段时遇到过同样的问题。

以下指令解决了 datagrid 字段的问题,该字段是一个 list(dicts 的 PersistentList (PersistentMappings))。

我想您可以根据您的情况调整此解决方案。

首先,您需要将以下代码添加到 getContent 方法中:

from plone.directives import form

class MyForm(form.SchemaEditForm):

    schema = IMyFormSchema
    ignoreContext = False

    def getContent(self):
        annotations = IAnnotations(self.context)
        if ANNOTATION_KEY not in annotations:
            annotations[ANNOTATION_KEY] = PersistentMapping()
        return YourStorageConfig(annotations[ANNOTATION_KEY])

重要说明:我包装注释存储以满足 z3c 表单的获取/设置行为。检查以下 YourStorageConfig 实现,您就会明白原因:-)。

class YourStorageConfig(object):
    implements(IMyFormSchema)

    def __init__(self, storage):
        self.storage = storage

    def __getattr__(self, name):
        if name == 'storage':
            return object.__getattr__(self, name)
        value = self.storage.get(name)
        return value

    def __setattr__(self, name, value):
        if name == 'storage':
            return object.__setattr__(self, name, value)
        if name == 'yourfieldname':
            self.storage[name] = PersistentList(map(PersistentMapping, value))
            return

        raise AttributeError(name)

yourfieldname 应该是您在表单架构中使用的字段名称。

要实现数据网格字段,还有一些工作要做,但这可能足以满足您的情况。

请发表评论或回溯,以便我可以提供进一步的帮助。如有必要,我会添加更多详细信息/解释;-)

关于plone - 如何将注释与 z3c.form 的 DictionaryField 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33305555/

相关文章:

plone - 如果您移动图像或目标页面,Plone 上的链接会更新吗? (4.3.4.1)

plone - Plone文件上传主要用在哪里?

workflow - 在 Plone 工作流的不同阶段分配来自不同组的审阅者

mysql - 如何上传文件(csv 或文本)并在 Zope 框架中读取内容

python - 使用 python 导出 zope 文件夹

plone - 无需扩建即可安装 ZOPE2

python - 将 collective.z3cform.datagridfield 与 plone.app.registry 和 GenericSetup 一起使用

indexing - 标题和描述未使用 Collective.dexteritytextindexer 建立索引

python - 在 Plone 中的字段上移动不变验证错误消息

plone - 如何为 z3cform 创建一个新的 contenttreewidget