[Zope3-Users] "Smarter" values for values = [] in Choice schema fields?

Hermann Himmelbauer dusty at qwer.tk
Thu Nov 6 11:49:47 EST 2008


Am Donnerstag 06 November 2008 17:30:27 schrieb Marius Gedminas:
> On Thu, Nov 06, 2008 at 02:13:56PM +0100, Hermann Himmelbauer wrote:
> > I quite often have Choice schema fields in my applications. In many
> > cases, these choice fields should have fixed values, thus I do it like
> > this:
> >
> > color = Choice( title=u"Color", values=['red', 'green', 'yellow'])
> >
> > My application then uses the these values for further processing, e.g.:
> >
> > if color == 'red': stop_traffic()
> >
> > The problem is, that often it is more appropriate to have one value for
> > display, and another for internal processing (e.g. when msgid strings are
> > involved, when the program needs specific values etc.)
> >
> > The only way I found is to set up a vocabulary and use
> > SimpleVocabulary.createTerm(key, n, name), however, that's quite tedious,
> > as I need to write quite some code, register the vocabulary etc.
>
> Not really.
>
> > So, perhaps there's a simpler solution? I'd favour something like this:
> >
> > color = Choice(titel=u"Color", values = [('red', 0, u"Red"), ('green', 1,
> > u"Green")....])
> >
> > Is that possible?
>
> Define a helper function
>
> def vocabulary(*terms):
>     return SimpleVocabulary([SimpleTerm(value, token, title)
>                              for value, token, title in terms])
>
> and use it
>
>     color = Choice(title=u"Color",
>                    vocabulary=vocabulary(
>                        (0, 'red', u'Red'),
>                        (1, 'green', u'Green'),
>                        ))

Ah, that's nice! I did not know that I can actually use a vocabulary object as 
parameter! Thanks!

Best Regards,
Hermann

-- 
hermann at qwer.tk
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope3-users mailing list