On Mon, Mar 25, 2013 at 4:54 PM, Dilip M <dilipm79@gmail.com> wrote:
Okay...Answering my own question. I did it using get_form_initial method like show below.
def get_form_initial(self, step):
if step == 'select':
initial={'release_id': 'Major.Minor.Patch', 'choose_items': (('1', 'Release1'), ('2','Release2'))}
return initial
return self.initial_dict.get(step, {})
But is this a correct way to do it? Can I used get_form_kwargs for this?
From the doc:
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 outputTypeError 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__'
Okay...Answering my own question. I did it using get_form_initial method like show below.
def get_form_initial(self, step):
if step == 'select':
initial={'release_id': 'Major.Minor.Patch', 'choose_items': (('1', 'Release1'), ('2','Release2'))}
return initial
return self.initial_dict.get(step, {})
But is this a correct way to do it? Can I used get_form_kwargs for this?
From the doc:
- WizardView.get_form_kwargs(step)
Returns a dictionary which will be used as the keyword arguments when instantiating the form instance on given step.
The default implementation:
def get_form_kwargs(self, step): return {}
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