Skip to main content

modelformset_factory

Return a FormSet class for the given Django model class.

def modelformset_factory(
model: Model,
form: ModelForm = ModelForm,
formfield_callback: callable = null,
formset: BaseModelFormSet = BaseModelFormSet,
extra: int = 1,
can_delete: boolean = false,
can_order: boolean = false,
max_num: int = null,
fields: list|str = null,
exclude: list = 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: int = null,
validate_min: boolean = false,
field_classes: dict = null,
absolute_max: int = null,
can_delete_extra: boolean = true,
renderer: BaseRenderer = null,
edit_only: boolean = false
) - > type

Return a FormSet class for the given Django model class.

Parameters

NameTypeDescription
modelModelThe Django model class that the formset will be responsible for managing and persisting.
formModelForm = ModelFormThe base form class used to create individual forms within the formset.
formfield_callbackcallable = nullA callable that takes a model field and returns a form field, used to customize field generation.
formsetBaseModelFormSet = BaseModelFormSetThe base class from which the returned formset class will inherit.
extraint = 1The number of empty, extra forms to display in the formset.
can_deleteboolean = falseIf True, adds a checkbox to each form to allow deleting the associated model instance.
can_orderboolean = falseIf True, adds a field to each form to allow reordering the instances.
max_numint = nullThe maximum number of forms that can be displayed in the formset.
fields`liststr` = null
excludelist = nullA list of model field names to exclude from the form.
widgetsdict = nullA dictionary mapping field names to custom widget classes or instances.
validate_maxboolean = falseIf True, validation will ensure the number of forms does not exceed max_num.
localized_fieldslist = nullA list of field names that should be localized (e.g., dates, numbers).
labelsdict = nullA dictionary mapping field names to custom verbose labels.
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_numint = nullThe minimum number of forms that must be present in the formset.
validate_minboolean = falseIf True, validation will ensure the number of forms is at least min_num.
field_classesdict = nullA dictionary mapping field names to custom form field classes.
absolute_maxint = nullA hard limit on the number of forms allowed to prevent memory exhaustion attacks.
can_delete_extraboolean = trueIf True, allows the deletion of extra forms that have not yet been saved.
rendererBaseRenderer = nullThe template renderer used to render the formset.
edit_onlyboolean = falseIf True, prevents the formset from creating new objects and only allows editing existing ones.

Returns

TypeDescription
typeA FormSet class configured to work with the specified Django model and form settings.