Django Boolean Field 返回 "---------" 作为值



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

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

提前感谢:)

您需要为project_type:指定一个默认值

project_type = models.CharField(max_length=50, choices=JOB_TYPES,
                                default=JOB_TYPES[0][0])

最新更新