polymorphic.formsets

This allows creating formsets where each row can be a different form type. The logic of the formsets work similar to the standard Django formsets; there are factory methods to construct the classes with the proper form settings.

The “parent” formset hosts the entire model and their child model. For every child type, there is an PolymorphicFormSetChild instance that describes how to display and construct the child. It’s parameters are very similar to the parent’s factory method.

Model formsets

polymorphic.formsets.polymorphic_modelformset_factory(model, formset_children, formset=<class 'polymorphic.formsets.models.BasePolymorphicModelFormSet'>, form=<class 'django.forms.models.ModelForm'>, fields=None, exclude=None, extra=1, can_order=False, can_delete=True, max_num=None, formfield_callback=None, widgets=None, validate_max=False, localized_fields=None, labels=None, help_texts=None, error_messages=None, min_num=None, validate_min=False, field_classes=None, child_form_kwargs=None)

Construct the class for an polymorphic model formset.

All arguments are identical to :func:’~django.forms.models.modelformset_factory’, with the exception of the ‘’formset_children’’ argument.

Parameters

formset_children (Iterable[PolymorphicFormSetChild]) – A list of all child :class:’PolymorphicFormSetChild’ objects that tell the inline how to render the child model types.

Return type

type

class polymorphic.formsets.PolymorphicFormSetChild(model, form=<class 'django.forms.models.ModelForm'>, fields=None, exclude=None, formfield_callback=None, widgets=None, localized_fields=None, labels=None, help_texts=None, error_messages=None)

Metadata to define the inline of a polymorphic child. Provide this information in the :func:’polymorphic_inlineformset_factory’ construction.

Inline formsets

polymorphic.formsets.polymorphic_inlineformset_factory(parent_model, model, formset_children, formset=<class 'polymorphic.formsets.models.BasePolymorphicInlineFormSet'>, fk_name=None, form=<class 'django.forms.models.ModelForm'>, fields=None, exclude=None, extra=1, can_order=False, can_delete=True, max_num=None, formfield_callback=None, widgets=None, validate_max=False, localized_fields=None, labels=None, help_texts=None, error_messages=None, min_num=None, validate_min=False, field_classes=None, child_form_kwargs=None)

Construct the class for an inline polymorphic formset.

All arguments are identical to :func:’~django.forms.models.inlineformset_factory’, with the exception of the ‘’formset_children’’ argument.

Parameters

formset_children (Iterable[PolymorphicFormSetChild]) – A list of all child :class:’PolymorphicFormSetChild’ objects that tell the inline how to render the child model types.

Return type

type

Generic formsets

polymorphic.formsets.generic_polymorphic_inlineformset_factory(model, formset_children, form=<class 'django.forms.models.ModelForm'>, formset=<class 'polymorphic.formsets.generic.BaseGenericPolymorphicInlineFormSet'>, ct_field='content_type', fk_field='object_id', fields=None, exclude=None, extra=1, can_order=False, can_delete=True, max_num=None, formfield_callback=None, validate_max=False, for_concrete_model=True, min_num=None, validate_min=False, child_form_kwargs=None)

Construct the class for a generic inline polymorphic formset.

All arguments are identical to generic_inlineformset_factory(), with the exception of the formset_children argument.

Parameters

formset_children (Iterable[PolymorphicFormSetChild]) – A list of all child PolymorphicFormSetChild objects that tell the inline how to render the child model types.

Return type

type

Low-level features

The internal machinery can be used to extend the formset classes. This includes:

polymorphic.formsets.polymorphic_child_forms_factory(formset_children, **kwargs)

Construct the forms for the formset children. This is mostly used internally, and rarely needs to be used by external projects. When using the factory methods (:func:’polymorphic_inlineformset_factory’), this feature is called already for you.

class polymorphic.formsets.BasePolymorphicModelFormSet(*args, **kwargs)

Bases: django.forms.models.BaseModelFormSet

A formset that can produce different forms depending on the object type.

Note that the ‘add’ feature is therefore more complex, as all variations need ot be exposed somewhere.

When switching existing formsets to the polymorphic formset, note that the ID field will no longer be named ‘’model_ptr’’, but just appear as ‘’id’’.

class polymorphic.formsets.BasePolymorphicInlineFormSet(data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None, **kwargs)

Bases: django.forms.models.BaseInlineFormSet, polymorphic.formsets.models.BasePolymorphicModelFormSet

Polymorphic formset variation for inline formsets

class polymorphic.formsets.BaseGenericPolymorphicInlineFormSet(data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None, **kwargs)

Bases: django.contrib.contenttypes.forms.BaseGenericInlineFormSet, polymorphic.formsets.models.BasePolymorphicModelFormSet

Polymorphic formset variation for inline generic formsets