非必需的SelectFieldWidget

时间:2014-07-25 07:17:37

标签: plone dexterity

我有一个灵巧的内容类型,我想要一个不需要的选择字段,哪些值来自词汇表。

这是词汇:

@grok.provider(IContextSourceBinder)
def voc_test(context):
    values = range(10, 21)
    terms = map(lambda x: SimpleTerm(value=str(x),
                                 title=str(x)), values)
    return SimpleVocabulary(terms)

这是该领域的定义:

from plone.directives import dexterity, form
from plone.namedfile.field import NamedImage
from zope import schema


class IMyType(form.Schema):     
    ... 
    form.widget('test', SelectFieldWidget)
    test = schema.List(
        title=_(u"Test"),
        value_type=schema.Choice(source=vocabularies.voc_test),
        description=_(u"desc_test"),
        required=False,
     )

我得到的是一个选择字段,其中包含来自词汇表的值,第一个值为“无值”。那样就好。但是当我点击“保存”并选择“无值”时,会显示错误消息:

Traceback (innermost last):

Module ZPublisher.Publish, line 138, in publish
Module ZPublisher.mapply, line 77, in mapply
Module ZPublisher.Publish, line 48, in call_object
Module plone.z3cform.layout, line 66, in __call__
Module plone.z3cform.layout, line 50, in update
Module plone.dexterity.browser.edit, line 52, in update
Module plone.z3cform.fieldsets.extensible, line 59, in update
Module plone.z3cform.patch, line 30, in GroupForm_update
Module z3c.form.group, line 145, in update
Module plone.app.z3cform.csrf, line 21, in execute
Module z3c.form.action, line 98, in execute
Module z3c.form.button, line 315, in __call__
Module z3c.form.button, line 170, in __call__
Module plone.dexterity.browser.edit, line 23, in handleApply
Module z3c.form.group, line 98, in extractData
Module z3c.form.form, line 147, in extractData
Module z3c.form.field, line 303, in extract
Module z3c.form.converter, line 316, in toFieldValue
Module z3c.form.term, line 41, in getValue
Module z3c.form.term, line 38, in getTermByToken
Module zope.schema.vocabulary, line 133, in getTermByToken
LookupError: --NOVALUE--

如果我改变:

required=False, 

required=True

保存工作。

希望有人可以提供帮助。感谢。

1 个答案:

答案 0 :(得分:2)

设置词汇表中的默认值并使该字段成为必需字段,以便“ - NOVALUE--”不在选项中。

如果出于某种原因,您希望对该默认值使用“ - NOVALUE - ”,则将其添加到词汇表中。如果设置了该字段,则不会重复该字段。