Skip to main content

inlineformset_factory

Return an InlineFormSet for the given kwargs.

``fk_name`` must be provided if ``model`` has more than one ``ForeignKey``
to ``parent_model``.
def inlineformset_factory(
parent_model: Model,
model: Model,
form: ModelForm = ModelForm,
formset: BaseInlineFormSet = BaseInlineFormSet,
fk_name: string = null,
fields: list|string = null,
exclude: list = null,
extra: integer = 3,
can_order: boolean = false,
can_delete: boolean = true,
max_num: integer = null,
formfield_callback: callable = null,
widgets: dict = null,
validate_max: boolean = false,
localized_fields: list = null,
labels: dict = null,
help_texts: dict = null,
error_messages: dict = null,
min_num: integer = null,
validate_min: boolean = false,
field_classes: dict = null,
absolute_max: integer = null,
can_delete_extra: boolean = true,
renderer: BaseRenderer = null,
edit_only: boolean = false
) - > class

Return an InlineFormSet for the given kwargs. fk_name must be provided if model has more than one ForeignKey to parent_model.

Parameters

NameTypeDescription
parent_modelModelThe model class that represents the 'one' side of the one-to-many relationship.
modelModelThe model class that represents the 'many' side of the relationship to be edited inline.
formModelForm = ModelFormThe form class used to render and validate individual model instances.
formsetBaseInlineFormSet = BaseInlineFormSetThe base class to use for the generated formset.
fk_namestring = nullThe name of the ForeignKey field on the model that points to the parent_model; required if multiple foreign keys exist.
fields`liststring` = null
excludelist = nullA list of field names to exclude from the form.
extrainteger = 3The number of empty, extra forms to display for adding new objects.
can_orderboolean = falseIf true, adds a field to each form to allow ordering of the objects.
can_deleteboolean = trueIf true, adds a checkbox to each form to allow deleting the object.
max_numinteger = nullThe maximum number of forms to display; automatically set to 1 if the foreign key is unique.
formfield_callbackcallable = nullA function that takes a model field and returns a form field.
widgetsdict = nullA dictionary mapping field names to custom widget classes or instances.
validate_maxboolean = falseIf true, validation will fail if the number of forms exceeds max_num.
localized_fieldslist = nullA list of field names that should be localized.
labelsdict = nullA dictionary mapping field names to custom label strings.
help_textsdict = nullA dictionary mapping field names to custom help text strings.
error_messagesdict = nullA dictionary mapping field names to dictionaries of custom error messages.
min_numinteger = nullThe minimum number of forms that must be filled out.
validate_minboolean = falseIf true, validation will fail if the number of forms is less than min_num.
field_classesdict = nullA dictionary mapping field names to custom form field classes.
absolute_maxinteger = nullThe absolute maximum number of forms allowed, acting as a hard limit for memory protection.
can_delete_extraboolean = trueIf true, allows the deletion of extra forms.
rendererBaseRenderer = nullThe template renderer used to render the formset.
edit_onlyboolean = falseIf true, prevents the addition of new objects through the formset.

Returns

TypeDescription
classA FormSet class specifically configured to handle related objects via a foreign key relationship.