Search code examples
plonedexterity

LookupError on form render if RelationChoice field in custom Dexterity type is not required


I have some custom Dexterity types with RelationChoice fields. Everything works fine except that if I set the required property of the RelationChoice field to False, I get an error when trying to display the edit form.

This is how one of these custom types look like:

class ISceneFolder(model.Schema):
    """
    Container for Scene type items
    """

    rootScene = RelationChoice(
        title=_(u"Root Scene"),
        description=_(u"Scene that will be shown first on entering the app"),
        source=ObjPathSourceBinder(object_provides=IScene.__identifier__),
        required=False,
    )

The last part of the stack trace (which is quite longer) looks like this:

  Module zope.tales.tales, line 696, in evaluate
   - URL: /Users/joan/projects/ecorail/cms/eggs/z3c.form-3.1.1-py2.7.egg/z3c/form/browser/radio_input.pt
   - Line 8, Column 4
   - Expression: <PythonExpr (view.renderForValue(item['value']))>
   - Names:
      {'args': (),
       'context': <SceneFolder at /cms/dev-workspace/dummy-map/scenes>,
       'default': <object object at 0x1065dabc0>,
       'loop': {},
       'nothing': None,
       'options': {},
       'repeat': {},
       'request': <HTTPRequest, URL=http://localhost:8080/cms/dev-workspace/dummy-map/scenes/@@edit>,
       'template': <zope.browserpage.viewpagetemplatefile.ViewPageTemplateFile object at 0x10a2a6790>,
       'view': <ContentTreeWidget 'form.widgets.rootScene'>,
       'views': <zope.browserpage.viewpagetemplatefile.ViewMapper object at 0x10b24dc50>}
  Module zope.tales.pythonexpr, line 59, in __call__
   - __traceback_info__: (view.renderForValue(item['value']))
  Module <string>, line 1, in <module>
  Module z3c.form.browser.radio, line 44, in renderForValue
  Module z3c.form.term, line 38, in getTermByToken
  Module zope.schema.vocabulary, line 133, in getTermByToken
LookupError: --NOVALUE--
> /Users/joan/projects/ecorail/cms/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/vocabulary.py(133)getTermByToken()
-> raise LookupError(token)

It looks to me like the template engine might be trying to render the current value of the field assuming that it belongs to the specified vocabulary, but it doesn't because there is no value given (but this fails even for elements that already have a chosen relation!). This happens to me when editing already existing elements of my custom class as well as when trying to add new ones.

Ironically, the examples of RelationChoice and RelationList in the Dexterity Developer Manual show the required attribute set to False, and the example for RelationChoice is pretty similar to my custom fields: http://docs.plone.org/external/plone.app.dexterity/docs/advanced/references.html

I'm using Plone 4.3, Dexterity 2.0.11, Relation Field 1.2.1 and Content tree widget 1.0.7.

I'm inclined to think this is just a bug, but maybe someone spots something that I'm missing before I report it.


Solution

  • Yes its a bug (Plone 4.3.3): dev.plone.org/ticket/14027 and github.com/zopefoundation/z3c.form/issues/17 - Pin down z3c.form to 3.0.5 in your buildout configuration (This workaround works for me):

    [versions]
    z3c.form = 3.0.5