Skip to main content

BaseFormSet

A collection of instances of the same Form class.

Attributes

AttributeTypeDescription
deletion_widgetWidget class = CheckboxInputThe widget class used for the deletion checkbox field on each form.
ordering_widgetWidget class = NumberInputThe widget class used for the numerical ordering field on each form.
default_error_messagesdictA dictionary containing default error messages for management form issues and form count validation.
template_name_divtemplate path = django/forms/formsets/div.htmlThe path to the template used for rendering the formset as a series of < div > elements.
template_name_ptemplate path = django/forms/formsets/p.htmlThe path to the template used for rendering the formset as a series of < p > elements.
template_name_tabletemplate path = django/forms/formsets/table.htmlThe path to the template used for rendering the formset as an HTML < table >.
template_name_ultemplate path = django/forms/formsets/ul.htmlThe path to the template used for rendering the formset as an unordered list (< ul >).

Constructor

Signature

def BaseFormSet(
data: dict = None,
files: dict = None,
auto_id: string = id_%s,
prefix: string = None,
initial: list = None,
error_class: class = ErrorList,
form_kwargs: dict = None,
error_messages: dict = None
) - > null

Parameters

NameTypeDescription
datadict = NoneA dictionary-like object containing the POST data.
filesdict = NoneA dictionary-like object containing uploaded files.
auto_idstring = id_%sThe format string used to generate HTML 'id' attributes.
prefixstring = NoneA prefix to be used for every form in the set to avoid name clashes.
initiallist = NoneA list of dictionaries containing initial data for the forms.
error_classclass = ErrorListThe class used to display errors.
form_kwargsdict = NoneAdditional keyword arguments to pass to each form instance.
error_messagesdict = NoneA dictionary of error messages to override the default ones.

Methods


management_form()

@classmethod
def management_form() - > [ManagementForm](managementform.md?sid=django_forms_formsets_managementform)

Return the ManagementForm instance for this FormSet.

Returns

TypeDescription
[ManagementForm](managementform.md?sid=django_forms_formsets_managementform)The form used to manage formset metadata like total and initial form counts

total_form_count()

@classmethod
def total_form_count() - > int

Return the total number of forms in this FormSet.

Returns

TypeDescription
intThe total number of forms to be displayed or processed, including extra forms

initial_form_count()

@classmethod
def initial_form_count() - > int

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

Returns

TypeDescription
intThe number of forms that were pre-filled with initial data

forms()

@classmethod
def forms() - > list

Instantiate forms at first property access.

Returns

TypeDescription
listA list of all Form instances in the formset

get_form_kwargs()

@classmethod
def get_form_kwargs(
index: int
) - > dict

Return additional keyword arguments for each individual formset form.

Parameters

NameTypeDescription
indexintThe index of the form being constructed, or None for an empty form

Returns

TypeDescription
dictA dictionary of keyword arguments to pass to the Form constructor

initial_forms()

@classmethod
def initial_forms() - > list

Return a list of all the initial forms in this formset.

Returns

TypeDescription
listThe subset of forms that contain initial data

extra_forms()

@classmethod
def extra_forms() - > list

Return a list of all the extra forms in this formset.

Returns

TypeDescription
listThe subset of forms that were added as extra empty forms

empty_form()

@classmethod
def empty_form() - > [Form](../forms/form.md?sid=django_forms_forms_form)

Creates a new, empty form instance with a placeholder prefix for use in dynamic JavaScript templates.

Returns

TypeDescription
[Form](../forms/form.md?sid=django_forms_forms_form)A blank Form instance with the prefix set to 'prefix'

cleaned_data()

@classmethod
def cleaned_data() - > list

Return a list of form.cleaned_data dicts for every form in self.forms.

Returns

TypeDescription
listA list of dictionaries containing validated data for each form

deleted_forms()

@classmethod
def deleted_forms() - > list

Return a list of forms that have been marked for deletion.

Returns

TypeDescription
listA list of Form instances where the deletion checkbox was checked

ordered_forms()

@classmethod
def ordered_forms() - > list

Return a list of form in the order specified by the incoming data. Raise an AttributeError if ordering is not allowed.

Returns

TypeDescription
listA list of Form instances sorted by their ordering field value

get_default_prefix()

@classmethod
def get_default_prefix() - > str

Returns the default string prefix used for form field names in the HTML.

Returns

TypeDescription
strThe default prefix string 'form'

get_deletion_widget()

@classmethod
def get_deletion_widget() - > type

Returns the widget class used for the deletion checkbox.

Returns

TypeDescription
typeThe widget class for deletion fields

get_ordering_widget()

@classmethod
def get_ordering_widget() - > type

Returns the widget class used for the ordering input.

Returns

TypeDescription
typeThe widget class for ordering fields

non_form_errors()

@classmethod
def non_form_errors() - > [ErrorList](../utils/errorlist.md?sid=django_forms_utils_errorlist)

Return an ErrorList of errors that aren't associated with a particular form -- i.e., from formset.clean(). Return an empty ErrorList if there are none.

Returns

TypeDescription
[ErrorList](../utils/errorlist.md?sid=django_forms_utils_errorlist)A collection of errors that apply to the formset as a whole

errors()

@classmethod
def errors() - > list

Return a list of form.errors for every form in self.forms.

Returns

TypeDescription
listA list of error dictionaries, one for each form in the set

total_error_count()

@classmethod
def total_error_count() - > int

Return the number of errors across all forms in the formset.

Returns

TypeDescription
intThe sum of non-form errors and all individual form errors

is_valid()

@classmethod
def is_valid() - > bool

Return True if every form in self.forms is valid.

Returns

TypeDescription
boolTrue if all non-deleted forms and the formset itself are valid

full_clean()

@classmethod
def full_clean() - > null

Clean all of self.data and populate self._errors and self._non_form_errors.

Returns

TypeDescription
nullNone

clean()

@classmethod
def clean() - > null

Hook for doing any extra formset-wide cleaning after Form.clean() has been called on every form. Any ValidationError raised by this method will not be associated with a particular form; it will be accessible via formset.non_form_errors()

Returns

TypeDescription
nullNone

has_changed()

@classmethod
def has_changed() - > bool

Return True if data in any form differs from initial.

Returns

TypeDescription
boolTrue if at least one form in the set has modified data

add_fields()

@classmethod
def add_fields(
form: [Form](../forms/form.md?sid=django_forms_forms_form),
index: int
) - > null

A hook for adding extra fields on to each form instance.

Parameters

NameTypeDescription
form[Form](../forms/form.md?sid=django_forms_forms_form)The form instance to which fields should be added
indexintThe index of the form within the formset

Returns

TypeDescription
nullNone

add_prefix()

@classmethod
def add_prefix(
index: int|str
) - > str

Constructs a prefixed index string for a form's fields to ensure uniqueness within the formset.

Parameters

NameTypeDescription
index`intstr`

Returns

TypeDescription
strA string in the format '{prefix}-{index}'

is_multipart()

@classmethod
def is_multipart() - > bool

Return True if the formset needs to be multipart, i.e. it has FileInput, or False otherwise.

Returns

TypeDescription
boolTrue if any form in the set requires a multipart encoding

media()

@classmethod
def media() - > [Media](../widgets/media.md?sid=django_forms_widgets_media)

Returns the media (CSS/JS) required by the forms in this formset.

Returns

TypeDescription
[Media](../widgets/media.md?sid=django_forms_widgets_media)A Media object containing the assets for the first form or empty form

template_name()

@classmethod
def template_name() - > str

Returns the name of the template used to render the formset.

Returns

TypeDescription
strThe path to the formset template

get_context()

@classmethod
def get_context() - > dict

Returns the template context dictionary for rendering the formset.

Returns

TypeDescription
dictA dictionary containing the 'formset' instance