我有一个由查询集生成的表单中的 ModelMultipleChoiceField。
class NewEvaluationPriorityForm(forms.Form):
priority_field = forms.ModelMultipleChoiceField(
queryset=None,
widget=forms.CheckboxSelectMultiple,
required=False
)
def __init__(self, user_type, qi_list, hgiours_list, is_hgiours_evaluation, school, *args, **kwargs):
super(NewEvaluationPriorityForm, self).__init__(*args, **kwargs)
if is_hgiours_evaluation is True:
priorities = Priority.objects.get_new_hgiours_evaluation_priorities(
hgiours_list,
school
)
else:
priorities = Priority.objects.get_new_evaluation_priorities(
qi_list,
user_type,
school
)
self.fields['priority_field'].queryset = priorities
self.fields['priority_field'].label = ''
我想要做的是使用列表设置字段中不同复选框的初始值。我尝试了以下方法(我知道查询集中有 5 个对象):
self.fields['priority_field'].initial = [True, False, True, False, True]
但这不起作用。
我需要做什么?