BaseInlineFormSet
A formset for child objects related to a parent.
Attributes
| Attribute | Type | Description |
|---|---|---|
| instance | Model instance | The parent model instance to which the child formset objects are related. |
| save_as_new | boolean = False | Boolean flag that, when True, treats existing objects as new entries by clearing primary and foreign key data during form construction. |
| unique_fields | set | A set containing the foreign key field name to ensure it is included in uniqueness validation checks. |
Constructor
Signature
def BaseInlineFormSet(
data: Any = None,
files: Any = None,
instance: [Model](../../db/models/base/model.md?sid=django_db_models_base_model) = None,
save_as_new: boolean = False,
prefix: string = None,
queryset: [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) = None,
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| data | Any = None | The data dictionary to bind to the formset. |
| files | Any = None | The file data to bind to the formset. |
| instance | [Model](../../db/models/base/model.md?sid=django_db_models_base_model) = None | The parent model instance the inline formset is attached to. |
| save_as_new | boolean = False | If True, treats existing objects as new ones to be saved. |
| prefix | string = None | A prefix to use for the formset fields. |
| queryset | [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) = None | The queryset of child objects to include in the formset. |
| **kwargs | dict | Additional keyword arguments passed to the parent BaseModelFormSet. |
Methods
initial_form_count()
@classmethod
def initial_form_count() - > integer
Calculates the number of forms that should be pre-populated with existing data. Returns 0 if the formset is configured to save all entries as new instances.
Returns
| Type | Description |
|---|---|
integer | The count of initial forms to display based on existing related objects. |
get_default_prefix()
@classmethod
def get_default_prefix() - > string
Generates a default string prefix for the formset based on the related model's accessor name. This ensures unique HTML field names when multiple formsets are present on a single page.
Returns
| Type | Description |
|---|---|
string | The sanitized accessor name of the foreign key relationship. |
save_new()
@classmethod
def save_new(
form: [ModelForm](modelform.md?sid=django_forms_models_modelform),
commit: boolean = True
) - > [Model](../../db/models/base/model.md?sid=django_db_models_base_model)
Saves a new model instance to the database while ensuring it is correctly linked to the current parent instance. This method updates the form instance with the latest parent state before performing the save.
Parameters
| Name | Type | Description |
|---|---|---|
| form | [ModelForm](modelform.md?sid=django_forms_models_modelform) | The form containing the data for the new object. |
| commit | boolean = True | Whether to persist the instance to the database immediately. |
Returns
| Type | Description |
|---|---|
[Model](../../db/models/base/model.md?sid=django_db_models_base_model) | The newly created model instance. |
add_fields()
@classmethod
def add_fields(
form: [ModelForm](modelform.md?sid=django_forms_models_modelform),
index: integer
) - > null
Adds specific fields to each form in the formset, including the InlineForeignKeyField that links the child to the parent. It handles logic for custom to_field relationships and auto-generated primary keys.
Parameters
| Name | Type | Description |
|---|---|---|
| form | [ModelForm](modelform.md?sid=django_forms_models_modelform) | The form instance to which the fields are being added. |
| index | integer | The position of the form within the formset. |
Returns
| Type | Description |
|---|---|
null | null |
get_unique_error_message()
@classmethod
def get_unique_error_message(
unique_check: tuple
) - > string
Constructs an error message for unique constraint violations, excluding the foreign key to the parent from the check. This prevents redundant error reporting for the relationship that defines the formset itself.
Parameters
| Name | Type | Description |
|---|---|---|
| unique_check | tuple | A collection of field names that compose the unique constraint being validated. |
Returns
| Type | Description |
|---|---|
string | A formatted error message describing the unique constraint violation. |