简体   繁体   English

如何在敏捷的数据网格字段中为选择字段传递列表值?

[英]How to pass list values for a choice field in a datagrid field in Dexterity?

I have datagridfield in my Dexterity content type, using collective.z3cform. 我的Dexterity内容类型中有datagridfield,它使用Collective.z3cform。 I have an interface classes, which defines the value_type as DictRow for the main datagridfield in my content type. 我有一个接口类,该接口类将我的内容类型中的主datagridfield的value_type定义为DictRow。

class IAssessment(interface):
    assessment_item=schema.Choice(
        title=u"Assessment Item",
        values=[u"Item 1",u"Item 2"],
        required=False
    )
    reference=schema.TextLine(title=u"Reference",required=False)


class Application(form.Schema,IImageScaleTraversable):
    form.widget(app_assessment=DataGridFieldFactory)
    app_assessment=schema.List(
        title=u"Application Assessment",
        value_type=DictRow(title=u"Application Assessment",schema=IAssessment)
    )

Is it possible to provide values for IAssessment under assessment_item field based on catalog query using portal_catalog from other content types. 是否可以使用来自其他内容类型的portal_catalog基于目录查询在评估_项目字段下为IAssessment提供值。 I got an errorr when I insert catalog=getToolByName(context, 'portal_catalog') since context is not defined, even if I put a parameter context in IAssessment, still I got the same error. 由于未定义上下文,因此在插入catalog = getToolByName(context,'portal_catalog')时出现错误提示,即使我将参数上下文放入IAssessment中,仍然出现相同的错误。 Is there a way I can do catalog query in an interface class, if not are there any alternative ways? 有没有其他方法可以在接口类中进行目录查询?

You most likely want a context source binder, which is a form of dynamic vocabulary . 您很可能需要上下文源活页夹,这是动态词汇表的一种形式。

from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import SimpleVocabulary
from Products.CMFCore.utils import getToolByName

@grok.provider(IContextSourceBinder)
def assessmentItems(context):
    catalog = getToolByName(context, 'portal_catalog')
    return SimpleVocabulary.fromItems(
        (result.getId(), result.getObject()) for result in catalog(...)
    )

In your field definition, you then pass this in as vocabulary : 在字段定义中,然后将其作为vocabulary传递:

assessment_item=schema.Choice(
    title=u"Assessment Item",
    vocabulary=assessmentItems,
    required=False
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM