Django Formset:对象没有属性“ get”

时间:2019-02-27 14:10:34

标签: python django django-forms formset django-formwizard

我想我已经快到了,但是最后一部分只是不想工作:(。我希望您能有所帮助,因为两天后我再也看不到它了。

在我的FormWizard中,我试图(1)基于上一组中的Slider Input(设置Extra值)显示一个Formset,以及(2)在想显示ChoiceField(表单.Select)基于上一步中的文本输入。

通过大量的stackoverflow搜索,我能够执行步骤1。步骤2几乎可以正常工作,除了ChoiceField不会使用Text输入中的新值更新之外。这是我在views.py中的代码:

class FormWizardView(LoginRequiredMixin, SessionWizardView):
    template_name = 'test/test.html'


    def get_form_initial(self, step):

        if step == 'formset_step':
            form_class = self.form_list[step]
            data = self.get_cleaned_data_for_step('slider_step')
            if data is not None:
                # To set the extra value in formset based on slider input
                extra = data['number_slider'] 
                form_class.extra = extra

                # To set the ChoiceField value in formset based on text-input
                form1_cleaned_data = self.get_cleaned_data_for_step('text_input_step')
                formset = form_class().forms
                for form in formset:
                    if form1_cleaned_data:
                        form.fields['text_input'].choices = [item for item in form1_cleaned_data.items()]
                        # Print form to test if the form updates
                        print(form)
                return formset

        return super(FormWizardView, self).get_form_initial(step)


    def done(self, form_list, **kwargs):
        do something

        return something

我正在尝试返回表单集,但出现错误'TestForm' object has no attribute 'get'。我可能在这里返回了错误的内容,但是无论我尝试返回什么,它都行不通。返回super(FormWizardView, self).get_form_initial(step)只会给我一个空的ChoiceField,而返回表格则会给我一个错误object of type 'TestForm' has no len()

我还在控制台中打印了该表格,看来工作正常。有谁知道我应该返回什么才能得到填充的ChoiceField?

非常感谢!

编辑: 感谢您的回答!当我修改get_form时:

def get_form(self, step=None, data=None, files=None):
        if step == 'formset_step':
            form_class = self.form_list[step]
            data = self.get_cleaned_data_for_step('slider_step')
            if data is not None:
                # To set the extra value in formset based on slider input
                extra = data['number_slider'] 
                form_class.extra = extra

                # To set the ChoiceField value in formset based on text-input
                form1_cleaned_data = self.get_cleaned_data_for_step('text_input_step')
                formset = form_class().forms
                for form in formset:
                    if form1_cleaned_data:
                        form.fields['text_input'].choices = [item for item in form1_cleaned_data.items()]
                        # Print form to test if the form updates
                        print(form)

        return super(FormWizardView, self).get_form(step, data, files)

我收到错误['ManagementForm data is missing or has been tampered with']。在浏览StackOverflow时,这似乎是一个模板问题(特别是没有设置{{ wizard.management_form }},但是我从Django FormTools文档中提取了正常工作的普通代码。在我的模板中,我有以下代码:

{% extends "base.html" %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
    {{ wizard.form.management_form }}
    {% for form in wizard.form.forms %}
        {{ form }}
    {% endfor %}
{% else %}
    {{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{%
˓→trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{%
˓→trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>
{% endblock %}

我在模板中看不到东西还是我的get_form函数不正确?非常感谢您查看我的问题:)

1 个答案:

答案 0 :(得分:0)

表单向导中的

方法get_form_initial应该返回一个字典,其中包含要创建的表单的初始数据,而不是表单本身。如果要修改整个表单,请尝试修改get_form方法。