我有一个python脚本,它返回一个基于上下文的,动态生成的简单列表:
def myVocabMethod(self):
mylist = ['a','list','apart']
# do sth dynamic
return mylist
我想将结果传递给selection-field,其词汇表属性为s.th.像这样:
atapi.StringField('theField'
vocabulary=.myPythonScript.myVocabMethod(),
(...)
),
如何将脚本结果和词汇值粘合在一起?
我找到的文档总是需要Grok。此外,它只是一个简单的列表,没有i18n或其他更复杂的功能。
答案 0 :(得分:1)
注册命名词汇表的Grokless方法:
http://developer.plone.org/forms/vocabularies.html#registering-a-named-vocabulary-provider-in-zcml
基本上你将它指向一个返回SimpleVocabulary实例的函数。
答案 1 :(得分:1)
帖子,我找到了我要找的东西就是这个: http://www.universalwebservices.net/web-programming-resources/zope-plone/dynamic-vocabularies-in-plone-archetypes/
在这里的官方文档中引用: http://developer.plone.org/content/archetypes/fields.html#dynamic-vocabularies
对于任何可能感兴趣的人,这是代码:
from Acquisition import aq_parent
from Products.Archetypes import atapi
from Products.Archetypes.public import DisplayList
YourArchetypeSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
atapi.StringField(
'somefieldname',
vocabulary = yourVocabulary,
),
))
class YourArchetype(base.ATCTContent):
def yourVocabulary(self):
dl = DisplayList()
# We can do something context-based here,
# f.e. get all Events in parent-folder:
eventlist = aq_parent(self).contentValues(filter={'portal_type' : 'Event'})
for event in eventlist:
dl.add(event['id'], event['title'])
return dl
atapi.registerType(YourArchetype, PROJECTNAME)