我希望我的自定义类型显示存储的词汇表标题。字段定义如下:
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
?
答案 0 :(得分: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)
更多信息: