gpt4 book ai didi

Django - CheckboxSelectMultiple 没有 "------"选择

转载 作者:行者123 更新时间:2023-12-02 03:36:24 24 4
gpt4 key购买 nike

如何从呈现的选项中删除“------”?我在我的模型表单中使用:

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

但这不是很优雅的方式:)

最佳答案

更新

类似的例子可能有用:

country = ModelChoiceField(reference_class = Country, choices= country_choices, 
required=True, empty_label=None, widget=forms.Select)

如果您想要解决方案客户端:

<script>     
$("#selectBox option[value='-----']").remove();
</script>

关于Django - CheckboxSelectMultiple 没有 "------"选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com