Hi,
I am writing a Plone 5.2 (Python 3.7) add-on for a Dexterity content type with a Choice field. The field is supposed to have autocomplete because there is a huge amount of possible choices (2k+) fetched from an external database. I used the AutocompleteFieldWidgeet
from plone.formwidget.autocomplete
, but I cannot get it to work correctly.
The issue is during the creation of the content object (if I edit an already existing one, all works fine): when I start typing on the field I get the following error:
Traceback (innermost last):
Module ZPublisher.WSGIPublisher, line 155, in transaction_pubevents
Module ZPublisher.WSGIPublisher, line 337, in publish_module
Module ZPublisher.WSGIPublisher, line 243, in publish
Module ZPublisher.BaseRequest, line 523, in traverse
Module ZPublisher.BaseRequest, line 330, in traverseName
Module zope.traversing.namespace, line 165, in namespaceLookup
Module plone.z3cform.traversal, line 54, in traverse
Module plone.dexterity.browser.add, line 141, in update
Module plone.z3cform.fieldsets.extensible, line 65, in update
Module plone.z3cform.patch, line 30, in GroupForm_update
Module z3c.form.group, line 141, in update
Module z3c.form.group, line 52, in update
Module z3c.form.group, line 48, in updateWidgets
Module z3c.form.field, line 277, in update
Module plone.app.z3cform.widget, line 443, in update
Module z3c.form.browser.text, line 36, in update
Module z3c.form.browser.widget, line 171, in update
Module Products.CMFPlone.patches.z3c_form, line 47, in _wrapped
Module z3c.form.widget, line 132, in update
Module plone.app.z3cform.converters, line 181, in toWidgetValue
Module plone.app.vocabularies.principals, line 147, in getTerm
Module plone.app.vocabularies.principals, line 113, in _get_term_from_source
ValueError: value or token must be provided (only one of those)
I wrote an example with a vocabulary similar to this. The code is in this github repository:
The relevant parts are the following:
The only way I found to fix this (as suggested here) is by monkey patching the method toWidgetValue
of plone.app.z3cform.converters.AjaxSelectFieldWidget
like this:
$ diff --color -U10 converters.py.bak converters.py
--- converters.py.bak 2020-04-27 15:01:56.209325019 +0200
+++ converters.py 2020-04-27 15:02:46.873695155 +0200
@@ -164,20 +164,22 @@
def toWidgetValue(self, value):
"""Converts from field value to widget tokenized widget value.
:param value: Field value.
:type value: list |tuple | set
:returns: Items separated using separator defined on widget
:rtype: string
"""
+ if value == (None,):
+ value = None
if not value:
return self.field.missing_value
vocabulary = self.widget.get_vocabulary()
tokenized_value = []
for term_value in value:
if vocabulary is not None:
try:
term = vocabulary.getTerm(term_value)
tokenized_value.append(term.token)
continue
If possible I'd like to avoid monkey patching. Is there a better way to fix this or some other widget that I could use?
Thank you,
Davide
Previous posts that might be related:
- https://community.plone.org/t/plone-5-2-rc5-issue-with-relationchoice-field/8776
- https://community.plone.org/t/how-to-get-ajaxselectfieldwidget-working-on-multi-selection-lists/4632
- https://community.plone.org/t/module-plone-formwidget-autocomplete-widget-line-63-in---call---attributeerror-simplevocabulary-object-has-no-attribute-search/3042/2
- https://community.plone.org/t/plone-5-2-1-plone-formwidget-autocomplete-how-can-i-get-autocompletefieldwidget-to-work-with-my-plone-5-2-1-installation/11599