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
| Attribute | Type | Description |
|---|---|---|
| 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_unique | boolean = False | A boolean flag that determines whether the model instance's unique constraints should be validated during the cleaning process. |
| _validate_constraints | boolean = False | A 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
| Name | Type | Description |
|---|---|---|
| data | Any = None | The data to be bound to the form. |
| files | Any = None | The file data to be bound to the form. |
| auto_id | str = id_%s | The format string used to generate HTML ID attributes. |
| prefix | str = None | A prefix to be prepended to all form field names. |
| initial | dict = None | A dictionary of initial values for the form fields. |
| error_class | type = ErrorList | The class used to display form errors. |
| label_suffix | str = None | A suffix to be appended to all form labels. |
| empty_permitted | bool = False | Whether the form is allowed to be empty. |
| instance | [Model](../../db/models/base/model.md?sid=django_db_models_base_model) = None | The model instance to be associated with the form. |
| use_required_attribute | bool = None | Whether to use the 'required' HTML attribute on inputs. |
| renderer | Any = None | The 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
| Type | Description |
|---|---|
dict | The 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
| Name | Type | Description |
|---|---|---|
| commit | boolean = True | Determines whether the instance is persisted to the database immediately |
Returns
| Type | Description |
|---|---|
[Model](../../db/models/base/model.md?sid=django_db_models_base_model) | The model instance created or updated by the form |