Fixed #16236 -- Added get_form_kwargs method to WizardView as a way to easily provide kwargs to each step form.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16398 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
3ee076b135
commit
b5e7eab9fd
|
@ -35,6 +35,12 @@ class Step2(forms.Form):
|
||||||
class Step3(forms.Form):
|
class Step3(forms.Form):
|
||||||
data = forms.CharField()
|
data = forms.CharField()
|
||||||
|
|
||||||
|
class CustomKwargsStep1(Step1):
|
||||||
|
|
||||||
|
def __init__(self, test=None, *args, **kwargs):
|
||||||
|
self.test = test
|
||||||
|
return super(CustomKwargsStep1, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
class UserForm(forms.ModelForm):
|
class UserForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
|
@ -48,6 +54,12 @@ class TestWizard(WizardView):
|
||||||
response = super(TestWizard, self).dispatch(request, *args, **kwargs)
|
response = super(TestWizard, self).dispatch(request, *args, **kwargs)
|
||||||
return response, self
|
return response, self
|
||||||
|
|
||||||
|
def get_form_kwargs(self, step, *args, **kwargs):
|
||||||
|
kwargs = super(TestWizard, self).get_form_kwargs(step, *args, **kwargs)
|
||||||
|
if step == 'kwargs_test':
|
||||||
|
kwargs['test'] = True
|
||||||
|
return kwargs
|
||||||
|
|
||||||
class FormTests(TestCase):
|
class FormTests(TestCase):
|
||||||
def test_form_init(self):
|
def test_form_init(self):
|
||||||
testform = TestWizard.get_initkwargs([Step1, Step2])
|
testform = TestWizard.get_initkwargs([Step1, Step2])
|
||||||
|
@ -102,6 +114,17 @@ class FormTests(TestCase):
|
||||||
response, instance = testform(request)
|
response, instance = testform(request)
|
||||||
self.assertEquals(instance.get_next_step(), 'step3')
|
self.assertEquals(instance.get_next_step(), 'step3')
|
||||||
|
|
||||||
|
def test_form_kwargs(self):
|
||||||
|
request = get_request()
|
||||||
|
|
||||||
|
testform = TestWizard.as_view([('start', Step1),
|
||||||
|
('kwargs_test', CustomKwargsStep1)])
|
||||||
|
response, instance = testform(request)
|
||||||
|
|
||||||
|
self.assertEqual(instance.get_form_kwargs('start'), {})
|
||||||
|
self.assertEqual(instance.get_form_kwargs('kwargs_test'), {'test': True})
|
||||||
|
self.assertEqual(instance.get_form('kwargs_test').test, True)
|
||||||
|
|
||||||
def test_form_prefix(self):
|
def test_form_prefix(self):
|
||||||
request = get_request()
|
request = get_request()
|
||||||
|
|
||||||
|
|
|
@ -354,6 +354,13 @@ class WizardView(TemplateView):
|
||||||
"""
|
"""
|
||||||
return self.instance_dict.get(step, None)
|
return self.instance_dict.get(step, None)
|
||||||
|
|
||||||
|
def get_form_kwargs(self, step=None):
|
||||||
|
"""
|
||||||
|
Returns the keyword arguments for instantiating the form
|
||||||
|
(or formset) on given step.
|
||||||
|
"""
|
||||||
|
return {}
|
||||||
|
|
||||||
def get_form(self, step=None, data=None, files=None):
|
def get_form(self, step=None, data=None, files=None):
|
||||||
"""
|
"""
|
||||||
Constructs the form for a given `step`. If no `step` is defined, the
|
Constructs the form for a given `step`. If no `step` is defined, the
|
||||||
|
@ -366,12 +373,13 @@ class WizardView(TemplateView):
|
||||||
if step is None:
|
if step is None:
|
||||||
step = self.steps.current
|
step = self.steps.current
|
||||||
# prepare the kwargs for the form instance.
|
# prepare the kwargs for the form instance.
|
||||||
kwargs = {
|
kwargs = self.get_form_kwargs(step)
|
||||||
|
kwargs.update({
|
||||||
'data': data,
|
'data': data,
|
||||||
'files': files,
|
'files': files,
|
||||||
'prefix': self.get_form_prefix(step, self.form_list[step]),
|
'prefix': self.get_form_prefix(step, self.form_list[step]),
|
||||||
'initial': self.get_form_initial(step),
|
'initial': self.get_form_initial(step),
|
||||||
}
|
})
|
||||||
if issubclass(self.form_list[step], forms.ModelForm):
|
if issubclass(self.form_list[step], forms.ModelForm):
|
||||||
# If the form is based on ModelForm, add instance if available.
|
# If the form is based on ModelForm, add instance if available.
|
||||||
kwargs.update({'instance': self.get_form_instance(step)})
|
kwargs.update({'instance': self.get_form_instance(step)})
|
||||||
|
|
|
@ -271,6 +271,16 @@ Advanced ``WizardView`` methods
|
||||||
def get_form_initial(self, step):
|
def get_form_initial(self, step):
|
||||||
return self.initial_dict.get(step, {})
|
return self.initial_dict.get(step, {})
|
||||||
|
|
||||||
|
.. method:: 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 {}
|
||||||
|
|
||||||
.. method:: WizardView.get_form_instance(step)
|
.. method:: WizardView.get_form_instance(step)
|
||||||
|
|
||||||
Returns a object which will be passed to the form for ``step`` as
|
Returns a object which will be passed to the form for ``step`` as
|
||||||
|
|
Loading…
Reference in New Issue