1

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

enter image description here

THANKS! :)

ivoruJavaBoy
  • 1,307
  • 2
  • 19
  • 39

1 Answers1

4

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
    ...
Tiny Instance
  • 2,351
  • 17
  • 21
  • Updated as a list. Your's have some curly brackets, why? – Tiny Instance Jul 22 '16 at 09:26
  • You only need the *values* for initialisation, you don't need the display names. – Tiny Instance Jul 22 '16 at 09:27
  • I'm not understanding... your sintax does not work for me... mine works for all the others values... – ivoruJavaBoy Jul 22 '16 at 09:44
  • Could you add it to give me an example to the list of values i'm passing to the form: 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'} – ivoruJavaBoy Jul 22 '16 at 09:47
  • 1
    Sorry for the syntax, I just fixed it. Keep everything as it is but try just changing the `'team_id': 'POCM_A09'` with `'team_id': ['POCM_A09', ]`. Because in your original question this `TEAM_ID_RETRIEVED = {('POCM_A09', 'POCM_A09')}` line was wrong I think. – Tiny Instance Jul 22 '16 at 10:34
  • It works... The only problem is that it does not override the list so the values are not easy to view...it just put in bold the ones i sent :( – ivoruJavaBoy Jul 22 '16 at 10:47
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118010/discussion-between-tiny-instance-and-ivorujavaboy). – Tiny Instance Jul 22 '16 at 10:50