Formsets and inlines

Formsets and Inlines support is based on the idea from the django-superform project. Formsets and Inlines are included and processed as normal Django form fields.

This approach keeps your view code free from form-specific details and allows you to use the same templating techniques as for all other fields in Django Viewflow.

Example

To use Formset and Inlines fields, you have to inherit from viewflow.forms.Form or viewflow.forms.ModelForm, and then define your forms and layouts as follows:

from django import forms
from django.forms import formset_factory
from viewflow.forms import Form

class AddressForm(forms.Form):
    line_1 = forms.CharField(max_length=250)
    line_2 = forms.CharField(max_length=250)
    state = forms.CharField(max_length=100)
    city = forms.CharField(max_length=100)
    zipcode = forms.CharField(max_length=10)

    layout = Layout(
        'line_1',
        'line_2',
        'state',
        Row('city', 'zipcode'),
    )

AddressFormSet = formset_factory(AddressForm, extra=3, can_delete=True)


class SignupForm(Form):
    username = forms.CharField(max_length=50)
    first_name = forms.CharField(max_length=250)
    last_name = forms.CharField(max_length=250)
    emails = FormSetField(formset_class=EmailFormSet)
    addresses = FormSetField(formset_class=AddressFormSet)

    layout = Layout(
        'username',
        Row('first_name', 'last_name'),
        'emails',
        Stacked(1, 'addresses'),
    )

In the above example, AddressForm is the base form for the AddressFormSet. We then create a SignupForm that includes the AddressFormSet as an Inline with the FormSetField class. The FormSetField class takes the AddressFormSet class as an argument. You can then customize the layout of the SignupForm with the Layout class, just like you would with a regular form.

When you render the form in a template, you can use the custom {% render %} tag to render the form and any Formsets or Inlines included in it.