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
| Name | Type | Description |
|---|---|---|
| model | Model | The Django model class that the formset will be responsible for managing and persisting. |
| form | ModelForm = ModelForm | The base form class used to create individual forms within the formset. |
| formfield_callback | callable = null | A callable that takes a model field and returns a form field, used to customize field generation. |
| formset | BaseModelFormSet = BaseModelFormSet | The base class from which the returned formset class will inherit. |
| extra | int = 1 | The number of empty, extra forms to display in the formset. |
| can_delete | boolean = false | If True, adds a checkbox to each form to allow deleting the associated model instance. |
| can_order | boolean = false | If True, adds a field to each form to allow reordering the instances. |
| max_num | int = null | The maximum number of forms that can be displayed in the formset. |
| fields | `list | str` = null |
| exclude | list = null | A list of model field names to exclude from the form. |
| widgets | dict = null | A dictionary mapping field names to custom widget classes or instances. |
| validate_max | boolean = false | If True, validation will ensure the number of forms does not exceed max_num. |
| localized_fields | list = null | A list of field names that should be localized (e.g., dates, numbers). |
| labels | dict = null | A dictionary mapping field names to custom verbose labels. |
| help_texts | dict = null | A dictionary mapping field names to custom help text strings. |
| error_messages | dict = null | A dictionary mapping field names to dictionaries of custom error messages. |
| min_num | int = null | The minimum number of forms that must be present in the formset. |
| validate_min | boolean = false | If True, validation will ensure the number of forms is at least min_num. |
| field_classes | dict = null | A dictionary mapping field names to custom form field classes. |
| absolute_max | int = null | A hard limit on the number of forms allowed to prevent memory exhaustion attacks. |
| can_delete_extra | boolean = true | If True, allows the deletion of extra forms that have not yet been saved. |
| renderer | BaseRenderer = null | The template renderer used to render the formset. |
| edit_only | boolean = false | If True, prevents the formset from creating new objects and only allows editing existing ones. |
Returns
| Type | Description |
|---|---|
type | A FormSet class configured to work with the specified Django model and form settings. |