Django Extra Views Documentation, Release 0.14.0
(continued from previous page)
{{ formset }}
...
<input type="submit" value="Submit" />
</form>
This view will render the template address_formset.html with a context variable formset representing the
AddressFormSet. Once POSTed and successfully validated, formset_valid will be called (which is where
your handling logic goes), then the view will redirect to success_url.
Formset constructor and factory kwargs
FormSetView exposes all the parameters you’d normally be able to pass to the django.forms.BaseFormSet
constructor and django.forms.formset_factory(). This can be done by setting the respective attribute on
the class, or formset_kwargs and factory_kwargs at the class level.
Below is an exhaustive list of all formset-related attributes which can be set at the class level for FormSetView:
...
from my_app.forms import AddressForm, BaseAddressFormSet
class AddressFormSetView(FormSetView):
template_name = 'address_formset.html'
form_class = AddressForm
formset_class = BaseAddressFormSet
initial = [{'type': 'home'}, {'type': 'work'}]
prefix = 'address-form'
success_url = 'success/'
factory_kwargs = {'extra': 2, 'max_num': None,
'can_order': False, 'can_delete': False}
formset_kwargs = {'auto_id': 'my_id_%s'}
In the above example, BaseAddressFormSet would be a subclass of django.forms.BaseFormSet.
2.2.2 ModelFormSetView
ModelFormSetView makes use of django.forms.modelformset_factory(), using the declarative syntax
used in FormSetView as well as Django’s own class-based views. So as you’d expect, the simplest usage is as
follows:
from extra_views import ModelFormSetView
from my_app.models import Item
class ItemFormSetView(ModelFormSetView):
model = Item
fields = ['name', 'sku', 'price']
template_name = 'item_formset.html'
Rather than setting fields, exclude can be defined at the class level as a list of fields to be excluded.
It is not necessary to define fields or exclude if a form_class is defined at the class level:
8 Chapter 2. Table of Contents