Skip to main content

modelform_factory

Return a ModelForm containing form fields for the given model. You can optionally pass a form argument to use as a starting point for constructing the ModelForm.

def modelform_factory(
model: Model,
form: ModelForm = ModelForm,
fields: list|str = None,
exclude: list = None,
formfield_callback: callable = None,
widgets: dict = None,
localized_fields: list = None,
labels: dict = None,
help_texts: dict = None,
error_messages: dict = None,
field_classes: dict = None
) - > ModelForm

Return a ModelForm containing form fields for the given model. You can optionally pass a form argument to use as a starting point for constructing the ModelForm.

Parameters

NameTypeDescription
modelModelThe Django model class that the factory will use to generate form fields.
formModelForm = ModelFormThe base class to use as a starting point for constructing the new ModelForm.
fields`liststr` = None
excludelist = NoneAn optional list of field names to exclude from the form, even if they are present in the fields list.
formfield_callbackcallable = NoneA callable that takes a model field and returns a custom form field instance.
widgetsdict = NoneA dictionary mapping model field names to specific UI widget classes.
localized_fieldslist = NoneA list of field names that should be enabled for localization.
labelsdict = NoneA dictionary mapping model field names to custom display labels.
help_textsdict = NoneA dictionary mapping model field names to custom help text strings.
error_messagesdict = NoneA dictionary mapping model field names to dictionaries of custom error messages.
field_classesdict = NoneA dictionary mapping model field names to specific form field classes.

Returns

TypeDescription
ModelFormA dynamically created ModelForm class tailored to the specified model and configuration.