2

私は Plone 4.1.4 を使用しており、schema.Choice の動的ソースを取得しようとしています。コンテキスト オブジェクトに依存する国リストを作成する必要があります。

私はこの例を使用しています: 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 ラッパーから継承する場所は、パーミッションまたはトラバーサルの問題でしょうか?

4

1 に答える 1

2

ソースはクラスであるため、インスタンス化する必要があります。

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

サブフォームや複雑なフォーム ウィジェット (リスト選択のためのウィジェット内のウィジェットなど) を使用する場合など、特定の状況では__parent__、Plone コンテキストに戻るために適切な外部コンテキストへのポインタをたどる必要があります。

于 2012-05-18T20:37:15.527 に答える