1

我正在创建一个基于模型的表单,其中我有一个布尔字段,如下所示:

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?我只在模型中指定了两个值。我该如何摆脱它?

提前致谢 :)

4

1 回答 1

0

您需要为 指定默认project_type

project_type = models.CharField(max_length=50, choices=JOB_TYPES,
                                default=JOB_TYPES[0][0])
于 2012-08-31T10:01:13.803 回答