如果我有一些选择变量:
In [1]: from model_utils import Choices
In [2]: CHOICES = Choices(
...: (1, 'somekey', 'The long title'),
...: )
In [3]: CHOICES[1]
Out[3]: 'The long title'
如何从中检索somekey
键?
This answer对我不起作用。
In [10]: {v: k for k, v in dict(CHOICES).iteritems()}
Out[10]: {'The long title': 1}
答案 0 :(得分:1)
解决方案是:
In [11]: {v: k for k, v in CHOICES._identifier_map.items()}
Out[11]: {1: 'somekey'}