Django FormWizard.as_view condition_dict callable called multiple times?

1k Views Asked by At

The following is a snippet of how I have setup my FormWizard. When I hit this view function, 'bar' gets printed once and 'foo' gets printed 7 times:

# views.py
def _show_repair_item_form_condition(wizard):
    print 'foo'
    return True

@login_required
def workshop(request):
    print 'bar'
    cw = WorkshopWizard.as_view([WorkshopRepairItemFormSet, EmptyForm], condition_dict={'0': _show_repair_item_form_condition})
    return cw(request)

I have looked at the implmentation of the as_view function and can't find any trace of a bug causing this to happen. There is no documentation on the web about this issue either.

Any ideas?

Thanks, Mike

1

There are 1 best solutions below

2
On

I know it's a late answer, but this is not a bug, and nothing to worry about. Looking up the call stack for the condition function, you'll find the calls in wizards/views.py: callable(condition) and of course condition = condition(self) both call the function. For the former, wizard.storage.current_step will be None. Here is the source code for Django 1.6:

def get_form_list(self):
    """
    This method returns a form_list based on the initial form list but
    checks if there is a condition method/value in the condition_list.
    If an entry exists in the condition list, it will call/read the value
    and respect the result. (True means add the form, False means ignore
    the form)

    The form_list is always generated on the fly because condition methods
    could use data from other (maybe previous forms).
    """
    form_list = SortedDict()
    for form_key, form_class in six.iteritems(self.form_list):
        # try to fetch the value from condition list, by default, the form
        # gets passed to the new list.
        condition = self.condition_dict.get(form_key, True)
        if callable(condition):
            # call the value if needed, passes the current instance.
            condition = condition(self)
        if condition:
            form_list[form_key] = form_class
    return form_list