[Zope3-dev] Schema & Vocabulary Fields

Gary Poster gary at zope.com
Wed Sep 10 11:56:35 EDT 2003



Roché Compaan wrote:
> I shouldn't have introduced the term 'references' now although this is
> were I am going. On a first pass I only want to define a field on a
> class that generates a widget on add/edit forms that provides me with a
> selection list that is dynamically populated eg. When I add/edit product
> X I am presented with a selection list or a set of checkboxes for the
> components that should be enabled on that product. The component options
> will most probably be returned by a query.

Makes sense.

> I thought this is such a common use case that there must be an existing 
> field and widget for this and vocabulary field seemed to be it.

Nope, not yet: ZC has such a thing for our Zope 2 work, based heavily on 
Fred's complex sample.  One of us (us including the community) needs to 
use that code to produce such a widget for Zope 3.  Then it will exist. 
:-)  We've contributed the vast majority of our work--essentially the 
generic Zope 3 part of it--to that complexsample directory.

Or maybe someone will use a completely different approach.  You can make 
any widget you want with this, and even swap out query sub-widgets 
separately on the basis of the vocabulary.

>>A vocabulary just lets you define the members of the set of possible
>>values.
> 
> But those members can be dynamic?

Absolutely.  Just fulfill the vocabulary interface, and you'll make the 
system happy.  In most cases, you're going to want to make sure that 
previously used values still are part of the vocabulary, but the complex 
sample even handles that case gracefully with the BrokenTerm.

>>For guidance on how to use vocabularies, though, you won't find much 
>>better than the complex sample Fred put in 
>>zope/app/browser/form/complexsample.  The vocabulary isn't dynamic, but 
>>you can see that a context is passed to it--it could easily use that 
>>context to do dynamic things.
> 
> Yes, I noticed but based on that example it just seems to much effort to
> do a really simple thing. In Formulator this can be done with a
> Multi*Widget and a single TALES expression.

Right, on two levels.

First, it is a lot of effort to do a simple thing because it is a 
*complex* sample.  It's intended to show complex stuff you can do.

Second, the complexsample widget is the majority of a generic widget 
solution.  It's a bit silly to have that infrastructure in there if the 
vocabulary is not dynamic.  It makes a lot more sense if the vocabulary 
is dynamic, and the new widget based on all this becomes a built-in part 
of Zope 3.

Once the widget I described above is built, all you'll need to do is 
define your vocabulary and you'll have a widget for it.  If you want a 
different widget, or a widget variation, write it and set it up in ZCML 
and you'll have that one available too.  Want to make a vocabulary that 
uses a TALES expression as it's data source?  Go for it!  You can even 
define the TALES expression in the ZCML vocabulary tag so that it is 
passed to the vocabulary whenever it is accessed by the vocabulary 
registry.  That would mean you wouldn't need to subclass the TALES-aware 
vocabulary class in order to get different data sources.

So: glass is half empty--the widgets don't exist yet, and complex 
vocabulary bases don't exist yet.  Glass is half full--the real meat of 
one approach to such a widget is waiting for someone with time and 
interest to hook it up.  ZC will probably get around to it, if no one 
else has, after we start working on commercial projects with Zope 3.

And dynamic vocabularies are really pleasantly easy to write, once you 
sit down to do it.

Zope X3: work in progress, with potential to spare.  ;-)

Gary





More information about the Zope3-dev mailing list