When using inline forms with form masks, where does form validation take place?

I am creating an example to learn more about using inline forms using SessionWizard. In the end, I want to integrate dynamic forms to add and remove individual forms through a template before submitting. However, when data is not available in the second form, it cannot be checked unlike the usual ModelForm.

Is there a method in SessionWizard that needs to be overridden? Is this something that is inherently handled in Django?

Guides and examples are welcome.

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], }) 

Master-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 %} 
+4
source share

All Articles