Skip to main content

BaseModelForm

This class provides the base functionality for creating forms that are tied to a specific database model. It handles the mapping of model fields to form fields, manages model instance creation or updates, and integrates model-level validation and constraints into the form's validation lifecycle. Key features include the ability to save the form data directly back to the model instance and support for deferred many-to-many relationship saving.

Attributes

AttributeTypeDescription
instance[Model](../../db/models/base/model.md?sid=django_db_models_base_model)The model instance associated with this form, used to populate initial data and receive updated values upon validation.
_validate_uniqueboolean = FalseA boolean flag that determines whether the model instance's unique constraints should be validated during the cleaning process.
_validate_constraintsboolean = FalseA boolean flag that determines whether the model instance's functional constraints should be validated during the cleaning process.

Constructor

Signature

def BaseModelForm(
data: Any = None,
files: Any = None,
auto_id: str = id_%s,
prefix: str = None,
initial: dict = None,
error_class: type = ErrorList,
label_suffix: str = None,
empty_permitted: bool = False,
instance: [Model](../../db/models/base/model.md?sid=django_db_models_base_model) = None,
use_required_attribute: bool = None,
renderer: Any = None
) - > null

Parameters

NameTypeDescription
dataAny = NoneThe data to be bound to the form.
filesAny = NoneThe file data to be bound to the form.
auto_idstr = id_%sThe format string used to generate HTML ID attributes.
prefixstr = NoneA prefix to be prepended to all form field names.
initialdict = NoneA dictionary of initial values for the form fields.
error_classtype = ErrorListThe class used to display form errors.
label_suffixstr = NoneA suffix to be appended to all form labels.
empty_permittedbool = FalseWhether the form is allowed to be empty.
instance[Model](../../db/models/base/model.md?sid=django_db_models_base_model) = NoneThe model instance to be associated with the form.
use_required_attributebool = NoneWhether to use the 'required' HTML attribute on inputs.
rendererAny = NoneThe renderer to use for form rendering.

Methods


clean()

@classmethod
def clean() - > dict

Sets flags to trigger model uniqueness and constraint validation and returns the cleaned data dictionary.

Returns

TypeDescription
dictThe dictionary of validated form data

validate_unique()

@classmethod
def validate_unique()

Call the instance's validate_unique() method and update the form's validation errors if any were raised.


validate_constraints()

@classmethod
def validate_constraints()

Call the instance's validate_constraints() method and update the form's validation errors if any were raised.


save()

@classmethod
def save(
commit: boolean = True
) - > [Model](../../db/models/base/model.md?sid=django_db_models_base_model)

Save this form's self.instance object if commit=True. Otherwise, add a save_m2m() method to the form which can be called after the instance is saved manually at a later time. Return the model instance.

Parameters

NameTypeDescription
commitboolean = TrueDetermines whether the instance is persisted to the database immediately

Returns

TypeDescription
[Model](../../db/models/base/model.md?sid=django_db_models_base_model)The model instance created or updated by the form