当使用带有表单向导的内联表单集时,表单集表单验证在哪里?

时间:2012-06-29 22:35:31

标签: inline-formset django-formwizard

我正在创建一个示例,以了解有关使用SessionWizard的内联表单集的更多信息。最后,我想集成动态表单集,以便在提交之前通过模板添加和删除单个表单。但是,当第二个表单中没有数据时,它不能像常规ModelForm那样进行验证。

SessionWizard中是否有需要覆盖的方法?这是Django内部处理的东西吗?

非常感谢指导和示例。

models.py

class Parent(models.Model):
    name = models.CharField(max_length=256)
    def __unicode__(self):
        return name

class Child(models.Model):
    name = models.CharField(max_length=256)
    parent = models.ForeignKey(Parent)
    def __unicode__(self):
        return name

urls.py

test_forms = [['parent', ParentForm],['child', ChildFormSet]]

urlpatterns = patterns('example.views',
    url(r'^$', TestWizard.as_view(test_forms)),
)

forms.py

class ParentForm(ModelForm):
    class Meta:
        model = Parent

class ChildForm(ModelForm):
    class Meta:
        model = Child
        exclude = ('parent',)

ChildFormSet = inlineformset_factory(Parent, Child, extra=1)

class TestWizard(SessionWizardView):
    """
    This WizardView is used to create multi-page forms and handles all the
    storage and validation stuff using sessions.
    """
    #template_name = ''

#    def get_template_names(self):
#        """
#        Returns a list of template names to be used for the request.
#        Overridden TemplateResponseMixin for specifying template for step.
#        """
#        return 'survey/forms/wizard_form.html'
#
#    def get_context_data(self, form, **kwargs):
#        context = super(TestWizard, self).get_context_data(form=form, **kwargs)
#        return context
#
#    def get_form_initial(self, step):
#        """
#        Returns dict (list of key, values) for initial form data.
#        Useful for populating form fields with data from prior form, with extra
#        logic for dealing with formsets.
#        """
#        return self.initial_dict.get(step, {})
#
#    def get_form(self, step=None, data=None, files=None):
#        """
#        Constructs the form for a given step - overridden to add extra arguments
#        """
#        form = super(TestWizard, self).get_form(step, data, files)
#        return form

    def done(self, form_list, **kwargs):
        return render_to_response('survey/thanks.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

向导form.html

{% extends "base.html" %}
{% load i18n %}

{% block head %}
    {{ wizard.form.media }}
{% endblock %}

{% block content %}
    <p>DEFAULT WIZARD FORM 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 %}

0 个答案:

没有答案