Django FormWizard.as_view condition_dict可调用多次调用?

时间:2012-08-17 13:35:40

标签: django formwizard

以下是我如何设置FormWizard的片段。当我点击这个视图功能时,'bar'被打印一次而'foo'被打印7次:

# 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)

我已经查看了as_view函数的实现,并且无法找到导致此情况发生的任何错误的跟踪。网络上没有关于此问题的文档。

有什么想法吗?

谢谢,迈克

1 个答案:

答案 0 :(得分:0)

我知道这是一个迟到的答案,但这不是一个错误,没有什么可担心的。查找条件函数的调用堆栈,您将在向导/ views.py中找到调用:callable(condition)当然condition = condition(self)都会调用该函数。对于前者,wizard.storage.current_step将为None。这是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