飞行和灵巧 : default values for "relation" field

标签 plone dexterity

在我的一个 Plone 站点中,我有一些用于生成字母的灵巧模型。模型是:“模型”(信件的基本内容)、“联系人”(包含联系信息,如姓名、地址等)和“合并”(这是一个渲染的模型对象,我们在其中替换了一些带有收件人信息的模型部分)。
“合并”对象的架构如下:

class IMergeSchema(form.Schema):
    """
    """
    title = schema.TextLine(
        title=_p(u"Title"),
        )

    form.widget(text='plone.app.z3cform.wysiwyg.WysiwygFieldWidget')
    text = schema.Text(
        title=_p(u"Text"),
        required=False,
        )

    form.widget(recipients=MultiContentTreeFieldWidget)
    recipients = schema.List(
        title=_('label_recipients',
                 default='Recipients'),
        value_type=schema.Choice(
            title=_('label_recipients',
                      default='Recipients'),
            # Note that when you change the source, a plone.reload is
            # not enough, as the source gets initialized on startup.
            source=UUIDSourceBinder(portal_type='Contact')),
        )

    form.widget(model=ContentTreeFieldWidget)
    form.mode(model='display')
    model = schema.Choice(
        title=_('label_model',
                  default='Model'),
        source=UUIDSourceBinder(portal_type='Model'),
        )

创建新的“合并”对象时,我希望使用创建新对象的文件夹中的所有可用联系人预设“收件人”字段。
我按照 Martin Aspelli 的指南为字段添加默认值:http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors

它适用于文本输入字段,但我不能让它适用于“收件人”字段。生成默认值的方法如下(带有一些带有丑陋打印的调试信息,但稍后将删除它们;)):
@form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
    contacts =  [x for x in data.context.contentValues()
                 if IContact.providedBy(x)]
    paths =  [u'/'.join(c.getPhysicalPath()) for c in contacts]
    uids = [IUUID(c, None) for c in contacts]

    print 'Contacts: %s' % contacts
    print 'Paths: %s' % paths
    print 'UIDs: %s' % uids

    return paths

我试图直接返回对象,它们的相对路径(在添加 View 中,当访问“self.widgets['recipients'].value”时,我得到了这种类型的数据)它们的 UID,但没有任何解决方案作为任何效果。

我也尝试返回元组而不是列表甚至生成器,但仍然没有任何效果。

确实调用了该方法,因为我在实例日志中看到了痕迹。

最佳答案

我认为您需要获取相关内容的“int_id”。这就是灵巧关系字段存储关系信息的方式:

from zope.component import getUtility
from zope.intid.interfaces import IIntIds

@form.default_value(field=IMergeSchema['recipients'])
def all_recipients(data):
    contacts =  [x for x in data.context.contentValues()
                 if IContact.providedBy(x)]
    intids = getUtility(IIntIds)
    # The following gets the int_id of the object and turns it into
    # RelationValue
    values = [RelationValue(intids.getId(c)) for c in contacts]

    print 'Contacts: %s' % contacts
    print 'Values: %s' % values

    return values

关于飞行和灵巧 : default values for "relation" field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803334/

相关文章:

单独测试 Dexterity 内容创建

Plone:从 dexterity xml 模型中提取 i18n 字符串

plone - 使用 z3c.form 进行单元测试 buttonAndHandler

plone - Zope 3 浏览器能否覆盖 CMF 皮肤中的模板 :page?

python - 自定义 Plone Dexterity 工厂以创建子内容

plone - 将索引添加到自定义目录

authentication - 在plone站点上成功注册后立即登录

plone - 如何在没有上下文的情况下访问 "plone site object"?

plone - 如何从浏览器 View 访问 z3c.form 小部件设置

plone - 在 Plone 4 上具有 Dexterity 的自定义 AddForm 模板