Skip to main content

BaseModelFormSet

A FormSet for editing a queryset and/or adding new objects to it.

Attributes

AttributeTypeDescription
modelModel class = nullThe Django model class that this formset is responsible for editing and creating.
edit_onlyboolean = falseA boolean flag that, when set to True, prevents the formset from saving any new objects and only allows modifications to existing ones.
unique_fieldsset = set()Set of fields that must be unique among forms of this set.

Constructor

Signature

def BaseModelFormSet(
data: dict = None,
files: dict = None,
auto_id: string = id_%s,
prefix: string = None,
queryset: [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) = None,
initial: list = None,
kwargs: dict
)

Parameters

NameTypeDescription
datadict = NoneA dictionary of data used to bind the formset.
filesdict = NoneA dictionary of uploaded file data.
auto_idstring = id_%sThe format string used to generate the HTML 'id' attribute for form fields.
prefixstring = NoneA prefix to be applied to the name of every field in the formset.
queryset[QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) = NoneThe QuerySet of model instances to be used in the formset.
initiallist = NoneA list of dictionaries containing initial data for extra forms.
kwargsdictAdditional keyword arguments passed to the parent BaseFormSet class.

Methods


initial_form_count()

@classmethod
def initial_form_count() - > int

Return the number of forms that are required in this FormSet.

Returns

TypeDescription
intThe count of forms representing existing objects in the queryset or the base formset count.

get_queryset()

@classmethod
def get_queryset() - > [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset)

Returns the queryset of objects to be edited by the formset, applying default ordering if necessary to ensure consistency.

Returns

TypeDescription
[QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset)The queryset containing the objects managed by this formset.

save_new()

@classmethod
def save_new(
form: [ModelForm](modelform.md?sid=django_forms_models_modelform),
commit: bool = True
) - > [Model](../../db/models/base/model.md?sid=django_db_models_base_model)

Save and return a new model instance for the given form.

Parameters

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

Returns

TypeDescription
[Model](../../db/models/base/model.md?sid=django_db_models_base_model)The newly created and saved model instance.

save_existing()

@classmethod
def save_existing(
form: [ModelForm](modelform.md?sid=django_forms_models_modelform),
obj: [Model](../../db/models/base/model.md?sid=django_db_models_base_model),
commit: bool = True
) - > [Model](../../db/models/base/model.md?sid=django_db_models_base_model)

Save and return an existing model instance for the given form.

Parameters

NameTypeDescription
form[ModelForm](modelform.md?sid=django_forms_models_modelform)The form containing updated data for the object.
obj[Model](../../db/models/base/model.md?sid=django_db_models_base_model)The existing model instance to be updated.
commitbool = TrueWhether to persist the changes to the database immediately.

Returns

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

delete_existing()

@classmethod
def delete_existing(
obj: [Model](../../db/models/base/model.md?sid=django_db_models_base_model),
commit: bool = True
) - > null

Deletes an existing model instance.

Parameters

NameTypeDescription
obj[Model](../../db/models/base/model.md?sid=django_db_models_base_model)The model instance to be deleted from the database.
commitbool = TrueWhether to execute the deletion immediately.

Returns

TypeDescription
nullnull

save()

@classmethod
def save(
commit: bool = True
) - > list

Save model instances for every form, adding and changing instances as necessary, and return the list of instances.

Parameters

NameTypeDescription
commitbool = TrueWhether to persist changes to the database; if False, adds a save_m2m method to the formset.

Returns

TypeDescription
listA list of all model instances that were created or updated.

clean()

@classmethod
def clean() - > null

Hook for performing additional validation on the formset; currently triggers uniqueness validation across all forms.

Returns

TypeDescription
nullnull

validate_unique()

@classmethod
def validate_unique() - > null

Validates that the data across all forms satisfies model uniqueness constraints and date-based uniqueness constraints.

Returns

TypeDescription
nullnull

get_unique_error_message()

@classmethod
def get_unique_error_message(
unique_check: tuple
) - > string

Constructs an error message for a failed uniqueness check involving one or more fields.

Parameters

NameTypeDescription
unique_checktupleA collection of field names that failed the uniqueness validation.

Returns

TypeDescription
stringA localized error message describing the duplicate field data.

get_date_error_message()

@classmethod
def get_date_error_message(
date_check: tuple
) - > string

Constructs an error message for a failed unique_for_date (or month/year) constraint.

Parameters

NameTypeDescription
date_checktupleA tuple containing the model class, lookup type, field name, and date field name.

Returns

TypeDescription
stringA localized error message describing the date-based uniqueness conflict.

get_form_error()

@classmethod
def get_form_error() - > string

Returns the generic error message used for forms that contain duplicate values.

Returns

TypeDescription
stringA localized string instructing the user to correct duplicate values.

save_existing_objects()

@classmethod
def save_existing_objects(
commit: bool = True
) - > list

Updates or deletes objects that were already present in the queryset based on form data.

Parameters

NameTypeDescription
commitbool = TrueWhether to persist updates and deletions to the database.

Returns

TypeDescription
listA list of existing model instances that were updated.

save_new_objects()

@classmethod
def save_new_objects(
commit: bool = True
) - > list

Creates and saves new model instances from the extra forms in the formset that have changed.

Parameters

NameTypeDescription
commitbool = TrueWhether to persist the new objects to the database.

Returns

TypeDescription
listA list of the newly created model instances.

add_fields()

@classmethod
def add_fields(
form: [ModelForm](modelform.md?sid=django_forms_models_modelform),
index: int|null
) - > null

Add a hidden field for the object's primary key.

Parameters

NameTypeDescription
form[ModelForm](modelform.md?sid=django_forms_models_modelform)The form instance to which the primary key field should be added.
index`intnull`

Returns

TypeDescription
nullnull