0

我希望我的自定义类型显示存储的词汇的标题。字段定义如下所示:

atapi.LinesField(
    'member_field',
    searchable=1,
    index='KeywordIndex',
    multiValued=1,
    storage=atapi.AnnotationStorage(),
    vocabulary_factory='member_name',
    widget=AutocompleteWidget(
        label=_(u"Member Name"),
        description=_(u"Multiple Lines, One Per Line."),
        actb_timeout=-1,
        actb_expand_onfocus=0,
        actb_filter_bogus=0,
    ),
    enforceVocabulary=0,
),

词汇定义如下所示:

class member_name(object):
    implements(IVocabularyFactory)
    def __call__(self, context=None):
        items = (
            SimpleTerm(value='john', title=u'John Doe'),
            SimpleTerm(value='paul', title=u'Paul Smith'),
            ... ...
        )
        return SimpleVocabulary(items)
member_nameFactory = member_name()

相应的页面模板如下所示:

<div tal:define="mbrs context/member_field|nothing"
     tal:condition="mbrs">
Member List:
<span tal:repeat="mbr mbrs">
  <span tal:replace="mbr">Member Name</span>
  <span class="separator"
   tal:condition="not: repeat/mbr/end" tal:replace="string:, " />
</span>
</div>

仅显示值的示例结果如下所示Member List: paul , john:我怎样才能显示他们的标题,例如:Member List: Paul Smith , John Doe

4

1 回答 1

3

词汇表(Zope3 风格)只是命名的实用程序,你可以像这样检索它们:

from zope.component import getUtility
from zope.schema.interfaces import IVocabularyFactory

factory = getUtility(IVocabularyFactory, vocabularyname)
vocabulary = factory(self.context)

然后你可以像这样得到这个词的标题:

fieldvalue = self.context.getField('myfield').get(self.context)
term = vocabulary.getTerm(fieldvalue)

print "Term value is %s token is %s and title is %s" + (term.value, term.token, term.title)

更多信息

于 2012-02-12T15:14:17.533 回答