7

I'm able to dynamically call one form related to the data I chose from the step ealier.

But when I'm in the done method I can see the my form_list is remaining unchanged.

here is what I did :

def get_form_list(request, form_list=None):
    if form_list is None:
        form_list = [ProviderForm, DummyForm, ConsummerForm, DummyForm, \
                 ServicesDescriptionForm]
    return UserServiceWizard.as_view(form_list=form_list)(request)


class UserServiceWizard(SessionWizardView):
    instance = None

    def __init__(self, **kwargs):
        self.form_list = kwargs.pop('form_list')
        return super(UserServiceWizard, self).__init__(**kwargs)

    def get_form_instance(self, step):
        if self.instance is None:
            self.instance = UserService()
        return self.instance

    def get_context_data(self, form, **kwargs):
        data = self.get_cleaned_data_for_step(self.get_prev_step(
                                                    self.steps.current))
        if self.steps.current == '1':
            service_name = str(data['provider']).split('Service')[1]
            form = class_for_name('th_' + service_name.lower() + '.forms',
                                  service_name + 'ProviderForm')
            self.form_list['1'] = form #here my form is correctly change I can see 

        elif self.steps.current == '3':
            service_name = str(data['consummer']).split('Service')[1]
            form = class_for_name('th_' + service_name.lower() + '.forms',
                                  service_name + 'ConsummerForm')
            self.form_list['3'] = form

        context = super(UserServiceWizard, self).get_context_data(form=form,
                                                              **kwargs)
        return context


    def done(self, form_list, **kwargs):
        print self.form_list #here form_list contains ProviderForm, DummyForm, ConsummerForm, DummyForm, ServicesDescriptionForm

at step 0 my form_list is ok :

{u'0': <class 'django_th.forms.wizard.ProviderForm'>, 
u'1': <class 'django_th.forms.wizard.DummyForm'>, 
u'2': <class 'django_th.forms.wizard.ConsummerForm'>, 
u'3': <class 'django_th.forms.wizard.DummyForm'>, 
u'4': <class 'django_th.forms.base.ServicesDescriptionForm'>}

at step 1 my form_list is ok : we can see the 2nd form is my expected one

{u'0': <class 'django_th.forms.wizard.ProviderForm'>, 
u'1': <class 'th_rss.forms.RssProviderForm'>, 
u'2': <class 'django_th.forms.wizard.ConsummerForm'>, 
u'3': <class 'django_th.forms.wizard.DummyForm'>, 
u'4': <class 'django_th.forms.base.ServicesDescriptionForm'>}

at step 2 my form_list is ko ; same as step 0 : my 2nd form is return to DummyForm

{u'0': <class 'django_th.forms.wizard.ProviderForm'>, 
u'1': <class 'django_th.forms.wizard.DummyForm'>, 
u'2': <class 'django_th.forms.wizard.ConsummerForm'>,
u'3': <class 'django_th.forms.wizard.DummyForm'>, 
u'4': <class 'django_th.forms.base.ServicesDescriptionForm'>}

How can I do to change self.form_list and keep the change I did in get_context_data until the end of the wizard and not at each step ?

EDIT here is the complete code that works fine with the Rohan's suggestion :

def get_form(self, step=None, data=None, files=None):
    """
        change the form instance dynamically from the data we entered
        at the previous step
    """
    if step is None:
        step = self.steps.current

    if step == '1':
        # change the form
        prev_data = self.get_cleaned_data_for_step('0')
        service_name = str(prev_data['provider']).split('Service')[1]
        class_name = 'th_' + service_name.lower() + '.forms'
        form_name = service_name + 'ProviderForm'
        form_class = class_for_name(class_name, form_name)
        form = form_class(data)
    elif step == '3':
        # change the form
        prev_data = self.get_cleaned_data_for_step('2')
        service_name = str(prev_data['consummer']).split('Service')[1]
        class_name = 'th_' + service_name.lower() + '.forms'
        form_name = service_name + 'ConsummerForm'
        form_class = class_for_name(class_name, form_name)
        form = form_class(data)
    else:
        # get the default form
        form = super(UserServiceWizard, self).get_form(step, data, files)
    return form

def done(self, form_list, **kwargs):
    """
        Save info to the DB
        The process is :
        1) get the infos for the Trigger from step 0, 2, 4
        2) save it to TriggerService
        3) get the infos from the "Provider" and "Consummer" services
        at step 1 and 3
        4) save all of them
    """
    # get the datas from the form for TriggerService
    i = 0
    for form in form_list:
        # cleaning
        data = form.cleaned_data
        # get the service we selected at step 0 : provider
        if i == 0:
            trigger_provider = UserService.objects.get(
                name=data['provider'],
                user=self.request.user)
            model_provider = get_service_model('provider', data)
        # get the service we selected at step 2 : consummer
        elif i == 2:
            trigger_consummer = UserService.objects.get(
                name=data['consummer'],
                user=self.request.user)
            model_consummer = get_service_model('consummer', data)
        # get the description we gave for the trigger
        elif i == 4:
            trigger_description = data['description']
        i += 1

    # save the trigger
    trigger = TriggerService(
        provider=trigger_provider, consummer=trigger_consummer,
        user=self.request.user, status=True,
        description=trigger_description)
    trigger.save()

    model_fields = {}
    # get the datas from the form for Service related
    # save the related models to provider and consummer
    i = 0
    for form in form_list:
        model_fields = {}
        data = form.cleaned_data
        # get the data for the provider service
        if i == 1:
            for field in data:
                model_fields.update({field: data[field]})
            model_fields.update({'trigger_id': trigger.id, 'status': True})
            model_provider.objects.create(**model_fields)
        # get the data for the consummer service
        elif i == 3:
            for field in data:
                model_fields.update({field: data[field]})
            model_fields.update({'trigger_id': trigger.id, 'status': True})
            model_consummer.objects.create(**model_fields)
        i += 1

    return HttpResponseRedirect('/')
4

2 回答 2

6

Instead of changing form list etc. in get_context_data(), I think more appropriate will be to implement get_form() method in your wizard view and return different form instance depending upon the step and previous data.

Something like this:

class UserServiceWizard(SessionWizardView):
    instance = None

    def get_form(self, step=None, data=None, files=None):
        if step is None:
            step = self.steps.current

        prev_data = self.get_cleaned_data_for_step(self.get_prev_step(
                                                    self.steps.current))
        if step == '1':
            service_name = str(prev_data['provider']).split('Service')[1]
            form_class = class_for_name('th_' + service_name.lower() + '.forms',
                                  service_name + 'ProviderForm')
            form = form_class(data)
        elif step == '3':
            service_name = str(prev_data['consummer']).split('Service')[1]
            form_class = class_for_name('th_' + service_name.lower() + '.forms',
                                  service_name + 'ConsummerForm')
            form = form_class(data)
        else:
            form = super(UserServiceWizard, self).get_form(step, data, files)

        return form

The trick here is do not change the length of form list (which you have done correctly), but just change form instance. Django has provided way to override get_form() method for this purpose. Django will honor this method and always use it to get the form instance for the method.

于 2013-10-06T06:38:32.300 回答
0

I'm not sure if it is the solution you are looking for, but if you modify form_list in process_step instead of in get_context_data it should work. You will have to change your code since process_step is executed after a form is submitted.

According to Django doc https://docs.djangoproject.com/en/1.5/ref/contrib/formtools/form-wizard/ process_step is the "Hook for modifying the wizard’s internal state", at least for self.kwargs vars (in fact your form_list is in self.kwargs["form_list"]) I have tested that all modifications in get_context_data are ignored so I think that self.form_list should behave in the same way.

于 2013-10-03T12:04:59.367 回答