3

以下是我如何设置 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 函数的实现,找不到任何导致这种情况发生的错误痕迹。网络上也没有关于此问题的文档。

有任何想法吗?

谢谢,迈克

4

1 回答 1

1

我知道这是一个迟到的答案,但这不是一个错误,没有什么可担心的。查看条件函数的调用堆栈,您会在wizards/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
于 2014-05-06T14:24:12.890 回答