Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango get_FOO_display and distinct()
    primarykey
    data
    text
    <p>I've seen answers to both halves of my question, but I can't work out how to marry the two.</p> <p>I have a book model, and a translatedBook model.</p> <p>The translatedBook has a langage set up as model choices in the usual way:</p> <pre><code>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'), </code></pre> <p>etc</p> <p>I know that to get "Italian" I have to do translatedBook.get_language_display on a Book object.</p> <p>But how do I get a list of <strong>distinct</strong> languages in their long format?</p> <p>I've tried:</p> <pre><code>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') </code></pre> <p>but I can't seem to get what I want - which is a list like:</p> <p>"English, Italian, Simplified Chinese, Spanish"</p> <p>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....</p> <p>-- EDIT --</p> <p>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:</p> <pre><code>langs = TargetText.objects.values_list('language', flat=True).distinct().order_by('language') </code></pre> <p>and it works. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload