[Zope3-Users] Referencing objects in auto-generated forms
Frank Burkhardt
fbo2 at gmx.net
Sat Apr 15 15:02:41 EDT 2006
Hi,
On Sat, Apr 15, 2006 at 08:41:57AM -0700, Ruben Gutierrez wrote:
> I'm having trouble understanding some of the semantics or conventions with
> Zope. One simple question I have is how do I create a list of selectable
> values in a form. I understand you can use a Choice object, but I would like
> to populate the list with all available objects of a specific type, within a
> specific container. For example, I have a form which requests an address.
> How do I populate the State (e.g., CA, HI, NY) field from a list of stored
> State objects? I don't want to include all states, as some may not be
> relevant (maybe the form is region-specific). But, I also don't want to
> statically list the states in a Choice declaration, either.
Sounds like a classic use case for a vocabulary.
Not tested:
*configure.zcml*
[...]
<utility
name="States"
factory="vocabularies.StatesFactory"
provides="zope.app.schema.interfaces.IVocabularyFactory"
/>
[...]
*vocabularies.py*
from zope.interface import implements
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from zope.app.schema.interfaces import IVocabularyFactory
class StatesFactory():
implements(IVocabularyFactory)
def __call__(self,context):
terms=[SimpleTerm(name,title=name) for name in context.keys()]
voc=SimpleVocabulary(terms)
return voc
The vocabulary factory makes use of the provided context object (which is
the current content object when editing a form. Just use the vocabulary in a
choice field:
*interfaces.py*
[...]
class IMyObject(Interface):
choice=Choice(
title=u'States',
vocabulary='States'
required=True
)
[...]
Regards,
Frank
More information about the Zope3-users
mailing list