[Zope3-Users] Vocabularies beyond SimpleVocabulary

Piotr Chamera piotr_chamera at poczta.onet.pl
Mon Jun 12 12:25:57 EDT 2006


Anders Bruun Olsen wrote:
> Hi,
> 
> I am trying to figure out how to implement vocabularies.
> My experimentation app is database of books which has a container for
> people and one for books. What I need to do is be able to form relations
> between these such that for a book I can choose from a list of the
> people in the database who the author(s) is(/are).
> The actual contentobjects and containers are SQLOS and
> SQLObjectContainers, but that shouldn't really matter too much I should
> think.
> 
> In my schema I then want to define a field which uses Choice that needs
> to take it's list possible values from a container.
> 
> I am thinking I just need to make my containers implement
> IVocabularyTokenized and my contentobjects implement
> ITitledTokenizedTerm, but I can't seem to figure out the correct way of
> doing this.
> I have read both doc/schema/vocabularies.txt and the chapters on
> vocabularies in Zope 3 Developers Handbook and Web Component Development
> with Zope 3, but they all use rather static sources (mostly just
> utilizing SimpleVocabulary) which doesn't really cover my needs.
> 
> Can anybody tell me the correct way to go about this when my source is a
> container instance in the ZODB?
> 

Hi.
I am just working on similar code. I'm beginner in zope and python, so I
  post this code for improvements from other users. I have defined
simple "factory" for my vocabularies (root folder is hardvired in the
code, it gets subfolder by name and creates title from given attribute).

"""
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

from zope.app import zapi
from zope.proxy import removeAllProxies

def getVocabulary(context, items_container, title_field):
     list = []
     root=zapi.getRoot(context)
     for (oid, oobj) in root.get("books").get(items_container).items():
         obj = removeAllProxies(oobj)
         list.append( SimpleTerm( obj, str(obj.__dict__[title_field]),
obj.__dict__[title_field]))
     return SimpleVocabulary( list )

def AuthorsVocabulary(context):
     return getVocabulary(context, "authors", "name")
"""

-- 
Piotr Chamera



More information about the Zope3-users mailing list