在Django MultipleChoiceField中设置默认选择

时间:2016-08-03 15:34:15

标签: python html django multiple-select

我在forms.py中设置了MultipleChoiceField,并想知道如何在用户打开表单时突出显示默认选项。

forms.py

def getCompReqNum():
    listCompNum = []

    all_reqs = Requisition.objects.distinct()
    for i in all_reqs:
        ....
        ....
    return tuple(listCompNum)

class CandidateForm(ModelForm)
...
    self.fields['req'] = forms.MultipleChoiceField(choices = getCompReqNum(), label = '* Requisition(s)')
    self.fields['req'].initial = "some default option"
...
widgets = {
'req_id'    : SelectMultiple()
 }

models.py

class Candidate(models.Model)
...
   req = models.ManyToManyField(Requisition, blank = True)
....

self.fields ['field_name'] .initial似乎没有为我设置默认值。是否有其他选项可以设置,以便在加载表单时,表单上已突出显示默认选项?下面是我想要完成的内容的截图。

Screenshot

HTML检查

<select multiple="multiple" id="id_req" name="req">
    <option value="56565">11-22 (sw2) (56565) </option>
</select>

0 个答案:

没有答案