Django get_FOO_display and distinct()
Posted
by datakid
on Stack Overflow
See other posts from Stack Overflow
or by datakid
Published on 2010-05-17T05:44:18Z
Indexed on
2010/05/17
5:50 UTC
Read the original article
Hit count: 263
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....
© Stack Overflow or respective owner