【问题标题】:Plone and Dexterity: default values for "relation" fieldPlone 和 Dexterity:“relation”字段的默认值
【发布时间】:2012-04-05 21:30:11
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: plone dexterity


    【解决方案1】:

    我认为您需要获取相关内容的“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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多