django形成隐藏的多选字段

时间:2014-08-05 11:36:42

标签: django django-forms

下面将如何隐藏除label ='student'之外的所有字段,

标签可以是学生,领导者,教师

for role in role_list:
            field_name = self.get_member_field_name(role.id)
            label = role.name  // student,leader,teacher

            self.fields[field_name] = forms.MultipleChoiceField(required=False,
                                                            label=label)
            self.fields[field_name].choices = users_list
            self.fields[field_name].initial = []

2 个答案:

答案 0 :(得分:4)

self.fields[field_name] = forms.MultipleChoiceField(
    required=False, 
    label=label, 
    widget=forms.MultipleHiddenInput()
)

答案 1 :(得分:-1)

使用HiddenInput小部件隐藏字段

如果标签不是学生:

self.fields[field_name] = forms.MultipleChoiceField(widget=forms.HiddenInput(),required=False,label=label)