Question

I have a form with a SelectFieldWidget, that is currently using a static vocabularly, which is basically this:

from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

primary_contacts = SimpleVocabulary([
    SimpleTerm( unicode(token), title=unicode(token.upper()), token=token ) for token in [
        'one','two','three','four','five','six','seven','eight','nine','ten',
    ]
])

The vocabulary is assigned to the field in the form schema:

form.widget( primary_contact_person=SelectFieldWidget )
primary_contact_person = schema.List(
    title = u'Nominate Primary Contact',
    required = False,
    value_type = schema.Choice(
        vocabulary=primary_contacts,
    )
)

The schema is then serialized using plone.supermodel & then deserialized when needed by the form (this is for another requirement).

The form is using a custom, handwritten template, and I'm in the process of adding the tal statements to generate the select field options. I had thought I could do this through referencing the widgets on the form, but when I do that I hit a problem:

(Pdb) self # break point in form
<Products.Five.metaclass.edit_metadata object at 0xc1ce450>
(Pdb) select = self.widgets['primary_contact_person']
(Pdb) first = [t for t in select.terms][0]
(Pdb) first.token
'one'
(Pdb) first.value
u'one'
(Pdb) first.title
(Pdb) 

The title is None on the term when it's accessed through the widget. I've tried looking it up through the vocabulary:

(Pdb) select.terms.getTermByToken('one').title
(Pdb)

But again, it's None. However, it is there for terms in the original vocabulary object:

(Pdb) from my.package import primary_contacts
(Pdb) [t for t in primary_contacts][0].title
u'ONE'

So while I could use the source vocab object directly to provide the values the template needs, the plan is for this vocabulary to eventually be dynamic, at which point I would expect I'd need to interrogate the widget itself.

What am I doing wrong here, why is title not being defined?

Was it helpful?

Solution

The problem was with plone.supermodel. I should have mentioned more clearly that I'm using the serialized schema to produce the form, and I apologise for this.

Basically, plone.supermodel provides an export/import process, which can only deal with simple lists of values.

# line 263 in plone.supermodel.exportimport
term = SimpleTerm(token = encoded, value = value, title = value)

The solution was to use named vocabularies, which serializes the reference to the vocabulary rather than the vocabulary itself.

Sorry again for the lack of information that made this harder to debug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top