【发布时间】:2012-03-19 00:46:38
【问题描述】:
如何从呈现的选项中删除“------”? 我以我的模型形式使用:
widgets = {
'event_form': forms.CheckboxSelectMultiple(),
}
在模型中,我有 IntegerField 可供选择:
EVENT_FORM_CHOICES = (
(1, _(u'aaaa')),
(2, _(u'bbbb')),
(3, _(cccc')),
(4, _(u'dddd')),
(5, _(eeee'))
)
呈现的选项包含 --------- 作为第一个可能的选项。我怎样才能摆脱它?
编辑: 我想出的唯一工作方式是(在 init 方法中):
tmp_choices = self.fields['event_form'].choices
del tmp_choices[0]
self.fields['event_form'].choices = tmp_choices
但这不是很优雅的方式:)
【问题讨论】:
-
event_form字段在您的模型中是什么样的? -
@Alasdair
event_form = models.PositiveSmallIntegerField(max_length=2, choices=EVENT_FORM_CHOICES) -
在下面查看我的答案。顺便说一句,我认为
max_length=2对IntegerField没有意义。
标签: django django-widget django-forms