Plone z3c.forms动态源提供程序将空字典作为上下文对象返回

Plone z3c.forms动态源提供程序将空字典作为上下文对象返回,plone,zope,dexterity,z3c.form,Plone,Zope,Dexterity,Z3c.form,我正在使用Plone 4.1.4,我正在尝试获取模式的动态源。要想工作,我需要填充国家列表,而国家列表又取决于上下文对象 我用这个例子: 例如,对于IContextSourceBinder,返回的是一个空字典,而不是实际的上下文对象: from zope import interface from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm from zope.schema.interfaces import ICont

我正在使用Plone 4.1.4,我正在尝试获取模式的动态源。要想工作,我需要填充国家列表,而国家列表又取决于上下文对象

我用这个例子:

例如,对于IContextSourceBinder,返回的是一个空字典,而不是实际的上下文对象:

from zope import interface
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from zope.schema.interfaces import IContextSourceBinder
import zope.schema
from z3c.form import form

class CountryGenerator(object):
    interface.implements(IContextSourceBinder)
    def __call__(self, context):
        #context is == {}
        import pdb; pdb.set_trace()
        return SimpleVocabulary([
            SimpleTerm(value="not_selected", title=_("Country Not Selected"))
                ])

class IStep(interface.Interface):
    region = schema.Choice(title=_("Select your country"),
                        required=True,
                        source=CountryGenerator,
                        default="not_selected")
class Step(form.Form):
    fields = field.Fields(IStep)
    label = _("Step")
    description = _("Select your country")
当在CountryGenerator中找到调试点时,调用方法并检查context对象,结果发现后者只是一个空字典

当我在上面提到的文章中尝试使用命名的实用程序示例时,类似的情况也发生了,上下文中也有{}

有人能指出我可能做错了什么吗

更新

调用表单的表单包装器的ZCML是

<browser:page
  name="view"
  for="Products.oldproduct.MyFolderishClass"
  class=".file.RegionClass"
  permission="zope2.View"
  />


RegionClass从表单包装器继承的地方,可能是权限问题还是遍历问题?

因为您的源是一个类,您需要实例化它:

class IStep(interface.Interface):
    region = schema.Choice(title=_("Select your country"),
                        required=True,
                        source=CountryGenerator(),
                        default="not_selected")

在某些情况下,例如使用子表单或复杂表单小部件(小部件中用于列表选择的小部件等),您需要按照
\uuuu parent\uuuuuu
指针指向适当的外部上下文,以返回到Plone上下文。

我最初认为这样做会同时初始化和调用对象,但事实似乎并非如此。我只是再试了一次,结果还是一样——上下文仍然是一个空字典。我进入堆栈,定义ChoiceTerms(上下文、请求、表单、字段、小部件):,我得到了正确的请求对象,但上下文是空的。这可能是ZCML slug的问题(请参阅我的更新)是的!这是一个应该是上下文的对象。这是怎么发生的?您的add表单还没有数据,但这就是它的上下文。按照
\uuuuu parent\uuuuuu
指针从那里开始。在堆栈跟踪中,\uuuu parent\uuuuuuuuu似乎工作得足够高,只有一些小部件似乎有它,所以我最终从zope.site.hooks调用getSite并使用它的上下文。我说得太快了,我再次传递了一个类而不是实例化的对象。现在我改变了这一点,回到了同一个问题,甚至getSite()都不起作用。