Reputation: 1357
I've a form, clicking the button "info" from a table, i get the user information and inject into the form, all of this works as well, but not for the 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})
this is my view code, the one I use to fill the form with the user values, every field have been valued correctly with the initial value, but not the last one, team_id.
An example of data to fill the team_id field is(This is the default list declared into the form):
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')}
And let's suppose i want to init it with just some values, the ones related to that user (this list has beene passed to the init mode, it does not work, it still takes the default list..)
TEAM_ID_RETRIEVED = {('POCM_A09', 'POCM_A09')}
This is the form:
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'
Any idea, i think should be something easy to fix... but i don't understand where is the problem...
For Tiny:
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)
it always shows
THANKS! :)
Upvotes: 1
Views: 623
Reputation: 2671
You only need to set the values, like:
initial = {
...
"team_id": ['POCM_A09', ...] # list of all the values selected
...
}
As per our chat discussion I'm updating the answer.
You can override the choices of the "MultipleChoiceField" inside form __init__()
method. First pass your new_choices
to the form, then:
def __init__(self, *args, **kwargs):
new_choices = kwargs.pop('new_choices', None)
super(FORM_NAME, self).__init__(*args, **kwargs)
...
self.fields['team_id'].choices = new_choices
...
Upvotes: 4