0

I am trying to fetch a Named Vocabulary and loop through its contents. Below is the code I have so far.

def get_car_types(self):

    car_types = []

    vtool = getToolByName(self, 'portal_vocabularies')
    cars_vocab = vtool.getVocabularyByName('my.package.car_models')

    for terms in cars_vocab:
        print terms.value + ": " + terms.title

I get the following error:

AttributeError: 'int' object has no attribute 'startswith'

It points out that the error is somewhere around this line:

for terms in cars_vocab:

What could be the problem? How does one fetch a named vocabulary and loop through the values? I have been using this link.

Frankline
  • 40,277
  • 8
  • 44
  • 75
  • Can you do a print cars_vocab, print type(cars_vocab) right before the for loop and get back with that additional info? – Bogdan Feb 28 '12 at 12:57
  • @Bogdan This is what I get: 2012-02-28 16:00:33 INFO my.product.browser – Frankline Feb 28 '12 at 13:02
  • 1
    When you report an error, please include the full traceback, not just the exception. That way we can pinpoint *where* your problem occurs. – Martijn Pieters Feb 29 '12 at 11:41

3 Answers3

2

In your loop "terms" is actually the string corresponding to the dictionary's key. Try this:

for value,term in cars_vocab.items():
        print value + ": " + term.title

Here value is the key string and term is a SimpleVocabularyTerm object.

Note: when asking for help about modular systems like Plone it is good to always specify any add-ons involved in the issue..(here notably Products.ATVocabularyManager)

Giacomo Spettoli
  • 4,476
  • 1
  • 16
  • 24
2

I figured out another way to make it work. Below is the code.

from Products.ATVocabularyManager import NamedVocabulary

def get_car_types(self):        

        car_types = []        
        car_vocab = NamedVocabulary('my.package.car_models')
        car_terms = car_vocab.getDisplayList(self).items()

        for term in car_terms:
            car_types.append( (term[0], term[1]) )

        return car_types

Thanks to all the guys who offered to answer my question.

Frankline
  • 40,277
  • 8
  • 44
  • 75
-1

You should be calling term instead of terms inside the loop. Also if terms.value is of type int you should surround it with str(). Try this:

print str(term.value) + ": " + str(term.title)
Lycha
  • 9,937
  • 3
  • 38
  • 43
  • It is not reaching that line. Read my question above. The error is in the line where the for-loop begins. – Frankline Feb 28 '12 at 13:04
  • Thank you. I have corrected my question above. But still that has not solved my problem yet. :-( – Frankline Feb 28 '12 at 13:27
  • How do you conclude that a AttributeError on `startswith` is due to an int being appended to a string? – Martijn Pieters Feb 29 '12 at 11:44
  • @MartijnPieters That was just a sidenote. I started that sentence with "Also...". I thought the AttributeError might come from using terms instead of term (he had that bug in his code earlier, he now has fixed that). – Lycha Feb 29 '12 at 11:53