如何从 model_utils 三重选择中获取密钥?
How to get key from model_utils triple Choices?
如果我有一些选择变量:
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}
解决方法是:
In [11]: {v: k for k, v in CHOICES._identifier_map.items()}
Out[11]: {1: 'somekey'}
如果我有一些选择变量:
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}
解决方法是:
In [11]: {v: k for k, v in CHOICES._identifier_map.items()}
Out[11]: {1: 'somekey'}