FormsetsΒΆ

New in version 1.0.

Polymorphic models can be used in formsets.

The implementation is almost identical to the regular Django formsets. As extra parameter, the factory needs to know how to display the child models. Provide a list of PolymorphicFormSetChild objects for this.

from polymorphic.formsets import polymorphic_modelformset_factory, PolymorphicFormSetChild

ModelAFormSet = polymorphic_modelformset_factory(ModelA, formset_children=(
    PolymorphicFormSetChild(ModelB),
    PolymorphicFormSetChild(ModelC),
))

The formset can be used just like all other formsets:

if request.method == "POST":
    formset = ModelAFormSet(request.POST, request.FILES, queryset=ModelA.objects.all())
    if formset.is_valid():
        formset.save()
else:
    formset = ModelAFormSet(queryset=ModelA.objects.all())

Like standard Django formsets, there are 3 factory methods available:

Each one uses a different base class:

When needed, the base class can be overwritten and provided to the factory via the formset parameter.