我有以下内容:
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保留“------”选项?
答案 0 :(得分:2)
如下:
manager = forms.CharField(required=False, widget=Select(choices=(('', '----------'),) + LIST), label='Their Position?')
答案 1 :(得分:0)
您需要使用ChoiceField
。