Well, unfortunately, there is no easy solution for this requirement. I once encountered the same problem using a datagrid field in the form of z3c.
The following statement solves the problem for the datagrid field, which is list
(PersistentList from dicts
(PersistentMappings).
I think you can adapt this solution for your business.
First you need to add the following code to the method getContent
:
from plone.directives import form
class MyForm(form.SchemaEditForm):
schema = IMyFormSchema
ignoreContext = False
def getContent(self):
annotations = IAnnotations(self.context)
if ANNOTATION_KEY not in annotations:
annotations[ANNOTATION_KEY] = PersistentMapping()
return YourStorageConfig(annotations[ANNOTATION_KEY])
Important Note. . I am moving the annotation repository to suit the get / set behavior of the z3c form. Check out the following implementation YourStorageConfig
and you will see why :-).
class YourStorageConfig(object):
implements(IMyFormSchema)
def __init__(self, storage):
self.storage = storage
def __getattr__(self, name):
if name == 'storage':
return object.__getattr__(self, name)
value = self.storage.get(name)
return value
def __setattr__(self, name, value):
if name == 'storage':
return object.__setattr__(self, name, value)
if name == 'yourfieldname':
self.storage[name] = PersistentList(map(PersistentMapping, value))
return
raise AttributeError(name)
yourfieldname
must be the name of the field that you use in the form layout.
To implement the datagrid field, you need to do one more work, but this may be enough for your case.
, , . /, -)