我有一个表格,单击表格中的“信息”按钮,我获取用户信息并注入表格,所有这些都有效,但不适用于MultipleChoiceField:
data = {'name': name_surname[0], 'surname': name_surname[1], 'c02': retrieved.c02, 'dep': retrieved.dept_no,
'job': retrieved.job_code, 'location': retrieved.c03, 'fbd_role': retrieved.c04, 'team_id': TEAM_ID_RETRIEVED}
form = RegForm(initial=data)
form.set_readonly()
return render(request, insert_form_address,
{'form': form, 'action': 'info', 'pk': pk, 'profiles': profile_list})
这是我的视图代码,我用来用用户值填写表单的代码,每个字段的初始值都正确赋值,但不是最后一个,team_id。
填充 team_id 字段的数据示例是(这是在表单中声明的默认列表):
TEAM_ID = {('POCM_A09', 'POCM_A09'),
('POCM_A11', 'POCM_A11'),
('POCM_A13', 'POCM_A13'),
('POCM_A15', 'POCM_A15'),
('POCM_A16', 'POCM_A16'),
('POCM_A18', 'POCM_A18')}
让我们假设我只想用一些值来初始化它,那些与该用户相关的值(这个列表已经传递给初始化模式,它不起作用,它仍然采用默认列表..)
TEAM_ID_RETRIEVED = {('POCM_A09', 'POCM_A09')}
这是表格:
class RegForm(forms.Form):
name = forms.CharField(label='Name', max_length=100)
surname = forms.CharField(label='Surname', max_length=100)
c02 = forms.CharField(label='AD ID', max_length=100)
dep = CustomModelChoiceField(label='Department', queryset=Department.objects.all())
fbd_role = forms.ChoiceField(label='FBD Role', choices=FBD_ROLES, initial=None)
location = forms.ChoiceField(label='Location', choices=LOCATION, initial=None)
job = forms.ChoiceField(label='Job', choices=JOBS)
## Unable to pass a initial value for this field..
team_id= forms.MultipleChoiceField(label='Team', choices=TEAM_ID)
action = None
user_id = None
def __init__(self, *args, **kwargs):
self.user_id = kwargs.pop('pk', None)
self.action = kwargs.pop('action', None)
super(RegForm, self).__init__(*args, **kwargs)
def set_readonly(self):
for field in self.fields:
self.fields[field].required = False
self.fields[field].widget.attrs['disabled'] = 'disabled'
任何想法,我认为应该很容易解决......但我不明白问题出在哪里......
对于小:
data = {'name': name_surname[0], 'surname': name_surname[1], 'c02': retrieved.c02, 'dep': retrieved.dept_no,
'job': retrieved.job_code, 'location': retrieved.c03, 'fbd_role': retrieved.c04, 'team_id': 'POCM_A09'}
form = RegForm(initial=data)
它总是显示
谢谢!:)