views:

81

answers:

1

I've seen answers to both halves of my question, but I can't work out how to marry the two.

I have a book model, and a translatedBook model.

The translatedBook has a langage set up as model choices in the usual way:

LANGUAGES = ( 
(u'it', u'Italian'),
(u'ja', u'Japanese'),
(u'es', u'Spanish'),
(u'zh-cn', u'Simplified Chinese'),
(u'zh-tw', u'Traditional Chinese'),
(u'fr', u'French'),
(u'el', u'Greek'),
(u'ar', u'Arabic'),
(u'bg', u'Bulgarian'),
(u'bn', u'Bengali'),

etc

I know that to get "Italian" I have to do translatedBook.get_language_display on a Book object.

But how do I get a list of distinct languages in their long format?

I've tried:

lang_avail = TargetText.objects.values('language').distinct().order_by('language')

lang_avail = TargetText.objects.distinct().order_by('language').values('language').

lang_avail = TargetText.objects.all().distinct('language').order_by('language')

but I can't seem to get what I want - which is a list like:

"English, Italian, Simplified Chinese, Spanish"

The final lang_avail listed above didn't return the list of 5, it returned the list of 355 (ie, # of books) with multiple repeats....

-- EDIT --

Daniel's answer almost got me there - as it turns out, that throws a "dicts are unhashable" error. Thanks to Axiak on the django irc, we use Daniel's solution with this line instead:

langs = TargetText.objects.values_list('language', flat=True).distinct().order_by('language')

and it works.

A: 

There isn't a built-in way. You could do something like this:

lang_dict = dict(LANGUAGES)
langs = TargetText.objects.values('language').distinct().order_by('language')
long_langs = [lang_dict[lang] for lang in langs]

which simply makes a dictionary out of the LANGUAGE choices and then looks up each language ID.

Daniel Roseman