2012-03-21 21 views
5

在我的一个Plone站点中,我有一些灵巧模型用于生成字母。这些模型是:“模型”(信件的基本内容),“联系人”(包含联系人信息,例如姓名,地址等)和“合并”(这是一个模型对象呈现,部分模型与收件人信息)。 “合并”对象的架构如下:Plone和敏捷:“relation”字段的默认值

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'), 
     ) 

当创建一个新的“合并”的对象,我想拥有的“收件人”字段与文件夹中所有可用的接触被预设在新对象被创建。 我跟着马丁阿斯佩里的指导,为一个字段添加默认值: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,但没有解决方案的任何影响。

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

当我在实例日志中看到痕迹时,肯定会调用该方法。

回答

3

我想你需要得到相关内容的“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