Skip to main content

BaseInlineFormSet

A formset for child objects related to a parent.

Attributes

AttributeTypeDescription
instanceModel instanceThe parent model instance to which the child formset objects are related.
save_as_newboolean = FalseBoolean flag that, when True, treats existing objects as new entries by clearing primary and foreign key data during form construction.
unique_fieldssetA 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

NameTypeDescription
dataAny = NoneThe data dictionary to bind to the formset.
filesAny = NoneThe file data to bind to the formset.
instance[Model](../../db/models/base/model.md?sid=django_db_models_base_model) = NoneThe parent model instance the inline formset is attached to.
save_as_newboolean = FalseIf True, treats existing objects as new ones to be saved.
prefixstring = NoneA prefix to use for the formset fields.
queryset[QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) = NoneThe queryset of child objects to include in the formset.
**kwargsdictAdditional 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

TypeDescription
integerThe 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

TypeDescription
stringThe 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

NameTypeDescription
form[ModelForm](modelform.md?sid=django_forms_models_modelform)The form containing the data for the new object.
commitboolean = TrueWhether to persist the instance to the database immediately.

Returns

TypeDescription
[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

NameTypeDescription
form[ModelForm](modelform.md?sid=django_forms_models_modelform)The form instance to which the fields are being added.
indexintegerThe position of the form within the formset.

Returns

TypeDescription
nullnull

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

NameTypeDescription
unique_checktupleA collection of field names that compose the unique constraint being validated.

Returns

TypeDescription
stringA formatted error message describing the unique constraint violation.