【问题标题】:z3c.forms dynamic sources provider returns empty dictionary as a context objectz3c.forms 动态源提供程序返回空字典作为上下文对象
【发布时间】:2012-06-28 19:47:41
【问题描述】:

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

我正在使用这个例子: http://plone.org/products/dexterity/documentation/manual/developer-manual/advanced/vocabularies

对于 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.__call__() 方法中命中调试点并检查上下文对象时,后者结果只是一个空字典。

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

谁能指出我可能做错了什么?

更新

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

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

RegionClass 继承自 Form wrapper 的地方,可能是权限问题还是遍历问题?

【问题讨论】:

    标签: plone zope dexterity z3c.form


    【解决方案1】:

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

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

    在某些情况下,例如使用子表单或复杂表单小部件(用于列表选择的小部件内的小部件等),您需要遵循 __parent__ 指向正确外部上下文的指针以返回 Plone 上下文.

    【讨论】:

    • 我最初以为这样做会同时初始化和调用对象,但似乎并非如此。我只是再次尝试了相同的结果——上下文仍然是一个空字典。我上了堆栈,到 def ChoiceTerms(context, request, form, field, widget):,我得到了正确的请求对象,但上下文是空的。会不会是 ZCML slug 的问题(请参阅我的更新)
    • 是的!这是一个应该是上下文的对象。这是怎么发生的?
    • 您的添加表单还没有数据,但这就是它的上下文。跟随__parent__ 指针并从那里开始。
    • __parent__ 似乎只在堆栈跟踪中工作得足够高,只有一些小部件似乎有它,所以我最终从 zope.site.hooks 调用 getSite 并使用它的上下文。
    • 我说得太早了,我一直在传递一个类而不是实例化对象。现在我改变了它并回到同样的问题,甚至 getSite() 都不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多