我正在尝试为管理员中的用户设置性别,但是会收到错误
Value u"[u'm']" is not a valid choice.
admin.py
class PlayerForm(forms.ModelForm):
GENDER_CHOICES = (
('m', 'Male'),
('f', 'Female'),
)
...
gender = forms.MultipleChoiceField(label="Gender", choices=GENDER_CHOICES)
...
class Meta:
model = Player
class PlayerAdmin(admin.ModelAdmin):
form = PlayerForm
admin.site.register(Player, PlayerAdmin)
models.py(玩家模型)
class Player(AbstractBaseUser):
GENDER_CHOICES = (
('m', 'Male'),
('f', 'Female'),
)
...
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
...
有趣的事实:表单会在admin中显示来自dadabase的正确默认值。
尝试提交表单时的POST Payload:
------WebKitFormBoundary009tVyo4cRJvIAnC
Content-Disposition: form-data; name="gender"
m
编辑:忘记提到数据库是Postgres 8.4并且有问题的列是
VARCHAR(1)
答案 0 :(得分:1)
由于您使用的是MultipleChoiceField
:
gender = forms.MultipleChoiceField(label="Gender", choices=GENDER_CHOICES)
它会将性别保存到“列表”中。导致:
>>> gender = ['m']
您需要使用用于选择单个内容的ChoiceField
:
gender = forms.ChoiceField(label="Gender", choices=GENDER_CHOICES)
导致:
>>> gender = 'm'