私は django query-fu のブラウンベルトにすぎません。ここには、私が知らない新しいトリックがいくつかあるかもしれません。
これが私のモデルです:
SCHOOL_TYPES = (('elementary', 'Elementary Schools'), ('middle', 'Middle Schools'), ('junior_high', 'Junior High Schools'), ('high_school', 'High Schools'), ('other', "Other"))
class School (BaseSlugModel):
name=CharField(max_length=100)
school_type = models.CharField(max_length=20, choices=SCHOOL_TYPES)
get_active_school_types
UI の目的で、 1 つ以上の学校が含まれる SCHOOL_TYPES タプルのサブセットを返す関数を作成したいと考えています。ブルートフォースよりも効率的にこれを行う方法はありますか?
編集:シメオンの答えに基づいた私の解決策は次のとおりです。
active_types = School.objects.values_list('school_type', flat=True).distinct()
return [ school_type for school_type in SCHOOL_TYPES if school_type[0] in active_types]