A FormSet for editing a queryset and/or adding new objects to it.
Attributes
| Attribute | Type | Description |
|---|
| model | Model class = null | The Django model class that this formset is responsible for editing and creating. |
| edit_only | boolean = false | A boolean flag that, when set to True, prevents the formset from saving any new objects and only allows modifications to existing ones. |
| unique_fields | set = 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
| Name | Type | Description |
|---|
| data | dict = None | A dictionary of data used to bind the formset. |
| files | dict = None | A dictionary of uploaded file data. |
| auto_id | string = id_%s | The format string used to generate the HTML 'id' attribute for form fields. |
| prefix | string = None | A 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) = None | The QuerySet of model instances to be used in the formset. |
| initial | list = None | A list of dictionaries containing initial data for extra forms. |
| kwargs | dict | Additional keyword arguments passed to the parent BaseFormSet class. |
Methods
@classmethod
def initial_form_count() - > int
Return the number of forms that are required in this FormSet.
Returns
| Type | Description |
|---|
int | The 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
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| form | [ModelForm](modelform.md?sid=django_forms_models_modelform) | The form containing data for the new object. |
| commit | bool = True | Whether to persist the changes to the database immediately. |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| 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. |
| commit | bool = True | Whether to persist the changes to the database immediately. |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| obj | [Model](../../db/models/base/model.md?sid=django_db_models_base_model) | The model instance to be deleted from the database. |
| commit | bool = True | Whether to execute the deletion immediately. |
Returns
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
| Name | Type | Description |
|---|
| commit | bool = True | Whether to persist changes to the database; if False, adds a save_m2m method to the formset. |
Returns
| Type | Description |
|---|
list | A 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
validate_unique()
@classmethod
def validate_unique() - > null
Validates that the data across all forms satisfies model uniqueness constraints and date-based uniqueness constraints.
Returns
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
| Name | Type | Description |
|---|
| unique_check | tuple | A collection of field names that failed the uniqueness validation. |
Returns
| Type | Description |
|---|
string | A 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
| Name | Type | Description |
|---|
| date_check | tuple | A tuple containing the model class, lookup type, field name, and date field name. |
Returns
| Type | Description |
|---|
string | A localized error message describing the date-based uniqueness conflict. |
@classmethod
def get_form_error() - > string
Returns the generic error message used for forms that contain duplicate values.
Returns
| Type | Description |
|---|
string | A 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
| Name | Type | Description |
|---|
| commit | bool = True | Whether to persist updates and deletions to the database. |
Returns
| Type | Description |
|---|
list | A 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
| Name | Type | Description |
|---|
| commit | bool = True | Whether to persist the new objects to the database. |
Returns
| Type | Description |
|---|
list | A 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
| Name | Type | Description |
|---|
| form | [ModelForm](modelform.md?sid=django_forms_models_modelform) | The form instance to which the primary key field should be added. |
| index | `int | null` |
Returns