繁体   English   中英

如何在z3c.form的DictionaryField中使用注释

[英]How to use annotations with z3c.form's DictionaryField

有关于使用Python dict with z3c.form (加载和存储表单数据)的文档

然而, z3c.form datamanager用于http://stardict.sourceforge.net/Dictionaries.php下载未注册的其它类型或接口(见参考 ),而注释通常使用类似PersistentDict

我如何使用DictionaryField datamanager在这种情况下? IE浏览器。 所以在我的表单的getContent方法中我只返回PersistentDict注释。

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

以下指令解决了datagrid字段的问题,该字段是一个listdicts of dicts (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表单的get / set行为。 检查以下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应该是您在表单架构中使用的字段名称。

要实现数据网格字段,还有一些工作要做,但这对您的情况来说已经足够了。

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

事实证明,答案就像下面的ZCML适配器注册一样简单:

<adapter
  for="persistent.dict.PersistentDict zope.schema.interfaces.IField"
  provides="z3c.form.interfaces.IDataManager"
  factory="z3c.form.datamanager.DictionaryField"
 />

有了这个,表单的以下自定义就足以使用( PersistentDict )注释来加载和存储表单数据:

def getContent(self):
   "return the object the form will manipulate (load from & store to)"
   annotations =  IAnnotations(self.context)
   return annotations[SOME_ANNOTATIONS_KEY_HERE]

这是假设一个PersistentDict先前已存储在annotations[SOME_ANNOTATIONS_KEY_HERE] -否则,上面的代码将导致KeyError 更改上面的getContent可能是一个好主意,这样如果注释尚不存在,则会创建并使用一些默认值进行初始化。

最后,请注意,由于某种原因, z3c.form 警告不要为每种映射类型启用DictionaryField ,因此例如对于表单存储子类PersistentDict可能是谨慎的,而不是直接使用它。 我向z3c.form提交了一个问题 ,要求澄清该警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM