Monday, March 25, 2013

Help with Dynamic ChoiceField

Hi,

I am trying to fill in choices for
MultipleChoiceField in runtime.  I am using formWizard and trying to return the dictionary using get_form_kwargs.

I am able set the initial for CharField but not choices for MultipleChoiceField.  I am trying to show up dynamic choices based on the user's input in first form. This choices will be calculated using form 1 cleaned_data.



views.py
--------

class SelectWizard(SessionWizardView):
   
    def get_form_initial(self, step):
        if step == 'select':
            initial={'release_id': 'Major.Minor.Patch'}
            return initial
        return self.initial_dict.get(step, {})
   

    def get_form_kwargs(self, step=None):
        if step == '
select':
            # Calculate the choices here from form 1 cleaned data

            '''
            cd = self.get_cleaned_data_for_step('main')

            '''

            return {'data': {'choices': (('1', 'Option1'), ('2','Option2'))},} # Manual for now

        return {}
   
    def done(self, form_list, **kwargs):
        # do something
        return HttpResponseRedirect('/queued/')



-----
forms.py

class Selection(forms.Form):

    def __init__(self, *args, **kwargs):
        print 'kwargs', kwargs
        super(
Selection, self).__init__(*args, **kwargs)
        self.fields['release_id'] = forms.CharField(initial=kwargs['initial']['release_id'])
        self.fields['choose_items'] = forms.MultipleChoiceField(choices=(('3', 'Option3'), ('4', 'Option4')))

        # choices_tuple = kwargs['data']['choices']
        # self.fields['choose_items'] = forms.MultipleChoiceField(choices=choices_tuple)
       
           
    release_id = forms.CharField()
    choose_items = forms.MultipleChoiceField()


-----


Console o/p.

kwargs {'files': None, 'prefix': 'select', 'initial': {'release_id': 'Major.Minor.Patch'}, 'data': None}


Django debugging output


TypeError at /

'NoneType' object has no attribute '__getitem__'
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 1.4.1
Exception Type: TypeError
Exception Value:
'NoneType' object has no attribute '__getitem__'

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment