Python django.forms 模块,BaseFormSet() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用django.forms.BaseFormSet()

项目:django-formtools-addons    作者:vikingco    | 项目源码 | 文件源码
def compute_form_list(cls, form_list, *args, **kwargs):
        computed_form_list = OrderedDict()

        assert len(form_list) > 0, 'at least one form is needed'

        # walk through the passed form list
        for i, form in enumerate(form_list):
            if isinstance(form, (list, tuple)):
                # if the element is a tuple, add the tuple to the new created
                # sorted dictionary.

                (step_name, form) = form
                if isinstance(form, dict):
                    form_mapping = form
                    computed_form_list[six.text_type(step_name)] = form_mapping
                elif isinstance(form, (list, tuple)):
                    form_mapping = OrderedDict(form)
                    computed_form_list[six.text_type(step_name)] = form_mapping
                elif issubclass(form, (forms.Form, forms.BaseForm, forms.BaseFormSet)):
                    computed_form_list[six.text_type(step_name)] = form
            else:
                # if not, add the form with a zero based counter as unicode
                computed_form_list[six.text_type(i)] = form

        # walk through the new created list of forms
        for form in six.itervalues(computed_form_list):
            form_collection = []
            if isinstance(form, dict):
                form_collection = form.values()
            elif issubclass(form, formsets.BaseFormSet):
                # if the element is based on BaseFormSet (FormSet/ModelFormSet)
                # we need to override the form variable.
                form = form.form
                form_collection = [form]

            for form in form_collection:
                # must test for BaseFormSet again in case form_collection
                # is a dict containing one.
                if issubclass(form, formsets.BaseFormSet):
                    # if the element is based on BaseFormSet (FormSet or
                    # ModelFormSet) we need to override the form variable.
                    form = form.form
                # check if any form contains a FileField, if yes, we need a
                # file_storage added to the wizardview (by subclassing).
                for field in six.itervalues(form.base_fields):
                    if (isinstance(field, forms.FileField) and
                            not hasattr(cls, 'file_storage')):
                        raise NoFileStorageConfigured(
                            "You need to define 'file_storage' in your "
                            "wizard view in order to handle file uploads.")

        return computed_form_list