DJANGO:表格中缺少空白选择选项

时间:2012-04-06 21:29:38

标签: python django django-forms widget modelform

我有以下内容:

MODEL.PY

LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),)

class Employee(models.Model):
    fname = models.CharField(max_length=15, blank=False, null=False)
    manager = models.CharField(max_length=15, choices=LIST, blank=True, null=True)
    comment = models.TextField(blank=True, null=True)
    def __unicode__(self):
        return self.fname

class EmployeeForm(forms.ModelForm):
    fname = forms.CharField(required=True, label='First Name')
    manager = forms.CharField(required=False, widget=Select(choices=LIST), label='Their Position?')
    comment = forms.CharField(required=False, widget=forms.Textarea(attrs={'cols': 50, 'rows': 10}), label='Comments')
    class Meta:
        model = Employee

如果我从EmployeeForm中删除管理器,我会在表单下拉菜单中选择“------”作为选项,但如果我将经理保留在EmployeeForm中(我需要为了覆盖字段标签),它删除“------”。如何让EmployeeForm保留“------”选项?

2 个答案:

答案 0 :(得分:2)

如下:

manager = forms.CharField(required=False, widget=Select(choices=(('', '----------'),) + LIST), label='Their Position?')

答案 1 :(得分:0)

您需要使用ChoiceField