我正在创建一个基于模型的表单,其中我有一个布尔字段:
project_type = models.CharField(max_length=50, choices=JOB_TYPES)
选择是:
JOB_TYPES = (
('fulltime', _('Fulltid')),
('project', _('Prosjektbasert')),
)
要将输入类型显示为单选按钮而不是,我这样做:
class AddJob(ModelForm):
class Meta:
model = Jobs
widgets = {
'project_type': RadioSelect(),
}
在HTML中,表单输出如下所示:
<label for="id_for_project_type_0">Project type</label>
<ul>
<li><label for="id_for_project_type_0"><input checked="checked" type="radio" id="id_for_project_type_0" value="" name="project_type" /> ---------</label></li>
<li><label for="id_for_project_type_1"><input type="radio" id="id_for_project_type_1" value="fulltime" name="project_type" /> Fulltid</label></li>
<li><label for="id_for_project_type_2"><input type="radio" id="id_for_project_type_2" value="project" name="project_type" /> Prosjektbasert</label></li>
</ul>
为什么我的值为0?我只在模型中指定了两个值。我该如何摆脱它?
提前致谢:)
答案 0 :(得分:0)
您需要为project_type
指定default value:
project_type = models.CharField(max_length=50, choices=JOB_TYPES,
default=JOB_TYPES[0][0])