I have a z3c form like this, that is not a dexterity content type :
from zope import schema
from plone.directives import form
from bsw.adel import personne
from plone.namedfile.field import NamedImage
from plone import namedfile
from plone.formwidget.namedfile import NamedImageFieldWidget
def get_portrait():
login = api.user.get_current().id
adel_personne = personne.Personne()
ma_personne = adel_personne.get(id_personne=None,
login=login)
if ma_personne is not None:
data = adel_personne.get_picture(id_personne=ma_personne.idPersonne,
photo_filename=None,
mode='v'
)
return namedfile.NamedImage(data=base64.b64decode(data), filename=u'portrait.jpg')
class ISouscripteur(form.Schema):
"""
Souscripteur Schema
"""
form.widget(portrait=NamedImageFieldWidget)
portrait = NamedImage(
title=_(u"Portrait"),
required=False,
defaultFactory=get_portrait
)
I can upload an image, that works. But when I load the form and get the portrait, I just see an empty input file. The data are comming from a external webservice.
I have tested to write an updateWidgets method to update the widget value with a NamedImage (according to the doc), but the result is the same.
With the debugger, I have found that the widget value still have a NamedImage in it, the updateWidgets method just replace it.
Is there is something I missed to show the image and the radio buttons in this widget ?
What you need is a computed field and, according to plone.directives.form documentation, z3c.form has the concept of a value adapter, a component that can provide a value for an attribute.
The following is an example for setting a title field with a default value using the first; try modifying it for you use case.