Skip to content
Back to top
View this page
Edit this page

# Formsets

Added 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:

- `polymorphic_modelformset_factory()` - create a regular model formset.

- `polymorphic_inlineformset_factory()` - create a inline model formset.

- `generic_polymorphic_inlineformset_factory()` - create an inline formset for a generic foreign key.

Each one uses a different base class:

- `BasePolymorphicModelFormSet`

- `BasePolymorphicInlineFormSet`

- `BaseGenericPolymorphicInlineFormSet`

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