How can I use Enums in Django
Formulating the question also helped to produce the answer.
Partly I want to share this answer for anyone with a similar question regarding using enums and choices, partly ask if the solution, for any reasons, is not advisable or other problems will occur further down the line (I thinking implement it on a larger scales several thousands of lines of tuples).
Below code serve two purposes in my project. 1. Building a table that can return an answer in a selected language 2. Enable a choice field of several choices (several columns) for e.g. the purpose to display various languages in a drop-down.
from enum import Enum
class MasterEnum(Enum):
# object # en # de
Product_HORSE = 'horse', 'pferd'
Product_CAR = 'car', 'auto'
Place_GARAGE = 'garage', 'garage'
Place_STABLE = 'stable', 'stall'
class YNNeutralEnum(Enum):
Yes = 'Yes', 'Ja'
No = 'No', 'Nein'
Neutral = 'Neutral', 'Neutral'
class Wessen(Enum):
W1 = 'Horse', 'Pferd'
W555 = 'Manbearpig', 'MannBärSchwein'
class Lang():
# column position in enum table
en = 0
de = 1
def ChoiceEnum(enum, Lang):
return tuple((x.name, x.value[Lang]) for x in enum)
# testing
choices = ChoiceEnum(Wessen, Lang.en)
print(choices)
choices = ChoiceEnum(MasterEnum, Lang.de)
print(choices)
print(choices[1])
print(Wessen.W555.name)
print(Wessen.W555.value)
print(Wessen.W555.value[1])