如何使用 get_FOO_display 访问 human-readable 名称
How to access the human-readable name using get_FOO_display
你好,我有下一个代码可以向我的参与者显示他们在前面session给我的回复文本。
J11 = models.IntegerField(
choices=[
[-2, 'Muy moralmente inapropiado'],
[-1, 'Moralmente inapropiado'],
[0, 'No aplica'],
[1, 'Moralmente apropiado'],
[2, 'Muy moralmente apropiado'],
],
widget=widgets.RadioSelect
)
TJ11 = models.CharField(max_length=2, choices=J11)
当我尝试证明此代码时,出现下一个错误:
File "C:\Users\diese\iat\incentivos\models.py", line 140, in <module>
class Player(BasePlayer):
File "C:\Users\diese\iat\incentivos\models.py", line 216, in Player
TJ11 = models.CharField(max_length=2, choices=J11)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site-packages\otree\db\models.py", line 386, in __init__super().__init__(max_length=max_length, **kwargs)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site-packages\otree\db\models.py", line 217, in __init__
fix_choices_arg(kwargs)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site-packages\otree\db\models.py", line 195, in fix_choices_arg
choices = expand_choice_tuples(choices)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site- packages\otree\common_internal.py", line 118, in expand_choice_tuples
if not isinstance(choices[0], (list, tuple)):
TypeError: 'IntegerField' object is not subscriptable
我能做什么?有人经历过同样的事情吗?
提前致谢
您可以在定义字段之前定义选项,然后将这些选项分配给字段。这允许您将这些选择分配给多个字段:
MY_CHOICES = (
(-2, 'Muy moralmente inapropiado'),
(-1, 'Moralmente inapropiado'),
(0, 'No aplica'),
(1, 'Moralmente apropiado'),
(2, 'Muy moralmente apropiado'),
)
J11 = models.IntegerField(choices=MY_CHOICES)
TJ12 = models.CharField(max_length=20, choices=MY_CHOICES)
你好,我有下一个代码可以向我的参与者显示他们在前面session给我的回复文本。
J11 = models.IntegerField(
choices=[
[-2, 'Muy moralmente inapropiado'],
[-1, 'Moralmente inapropiado'],
[0, 'No aplica'],
[1, 'Moralmente apropiado'],
[2, 'Muy moralmente apropiado'],
],
widget=widgets.RadioSelect
)
TJ11 = models.CharField(max_length=2, choices=J11)
当我尝试证明此代码时,出现下一个错误:
File "C:\Users\diese\iat\incentivos\models.py", line 140, in <module>
class Player(BasePlayer):
File "C:\Users\diese\iat\incentivos\models.py", line 216, in Player
TJ11 = models.CharField(max_length=2, choices=J11)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site-packages\otree\db\models.py", line 386, in __init__super().__init__(max_length=max_length, **kwargs)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site-packages\otree\db\models.py", line 217, in __init__
fix_choices_arg(kwargs)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site-packages\otree\db\models.py", line 195, in fix_choices_arg
choices = expand_choice_tuples(choices)
File "c:\users\diese\appdata\local\programs\python\python37\lib\site- packages\otree\common_internal.py", line 118, in expand_choice_tuples
if not isinstance(choices[0], (list, tuple)):
TypeError: 'IntegerField' object is not subscriptable
我能做什么?有人经历过同样的事情吗?
提前致谢
您可以在定义字段之前定义选项,然后将这些选项分配给字段。这允许您将这些选择分配给多个字段:
MY_CHOICES = (
(-2, 'Muy moralmente inapropiado'),
(-1, 'Moralmente inapropiado'),
(0, 'No aplica'),
(1, 'Moralmente apropiado'),
(2, 'Muy moralmente apropiado'),
)
J11 = models.IntegerField(choices=MY_CHOICES)
TJ12 = models.CharField(max_length=20, choices=MY_CHOICES)