A collection of instances of the same Form class.
Attributes
| Attribute | Type | Description |
|---|
| deletion_widget | Widget class = CheckboxInput | The widget class used for the deletion checkbox field on each form. |
| ordering_widget | Widget class = NumberInput | The widget class used for the numerical ordering field on each form. |
| default_error_messages | dict | A dictionary containing default error messages for management form issues and form count validation. |
| template_name_div | template path = django/forms/formsets/div.html | The path to the template used for rendering the formset as a series of < div > elements. |
| template_name_p | template path = django/forms/formsets/p.html | The path to the template used for rendering the formset as a series of < p > elements. |
| template_name_table | template path = django/forms/formsets/table.html | The path to the template used for rendering the formset as an HTML < table >. |
| template_name_ul | template path = django/forms/formsets/ul.html | The 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
| Name | Type | Description |
|---|
| data | dict = None | A dictionary-like object containing the POST data. |
| files | dict = None | A dictionary-like object containing uploaded files. |
| auto_id | string = id_%s | The format string used to generate HTML 'id' attributes. |
| prefix | string = None | A prefix to be used for every form in the set to avoid name clashes. |
| initial | list = None | A list of dictionaries containing initial data for the forms. |
| error_class | class = ErrorList | The class used to display errors. |
| form_kwargs | dict = None | Additional keyword arguments to pass to each form instance. |
| error_messages | dict = None | A dictionary of error messages to override the default ones. |
Methods
@classmethod
def management_form() - > [ManagementForm](managementform.md?sid=django_forms_formsets_managementform)
Return the ManagementForm instance for this FormSet.
Returns
| Type | Description |
|---|
[ManagementForm](managementform.md?sid=django_forms_formsets_managementform) | The form used to manage formset metadata like total and initial form counts |
@classmethod
def total_form_count() - > int
Return the total number of forms in this FormSet.
Returns
| Type | Description |
|---|
int | The total number of forms to be displayed or processed, including extra forms |
@classmethod
def initial_form_count() - > int
Return the number of forms that are required in this FormSet.
Returns
| Type | Description |
|---|
int | The number of forms that were pre-filled with initial data |
@classmethod
def forms() - > list
Instantiate forms at first property access.
Returns
| Type | Description |
|---|
list | A list of all Form instances in the formset |
@classmethod
def get_form_kwargs(
index: int
) - > dict
Return additional keyword arguments for each individual formset form.
Parameters
| Name | Type | Description |
|---|
| index | int | The index of the form being constructed, or None for an empty form |
Returns
| Type | Description |
|---|
dict | A dictionary of keyword arguments to pass to the Form constructor |
@classmethod
def initial_forms() - > list
Return a list of all the initial forms in this formset.
Returns
| Type | Description |
|---|
list | The subset of forms that contain initial data |
@classmethod
def extra_forms() - > list
Return a list of all the extra forms in this formset.
Returns
| Type | Description |
|---|
list | The subset of forms that were added as extra empty forms |
@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
| Type | Description |
|---|
[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
| Type | Description |
|---|
list | A list of dictionaries containing validated data for each form |
@classmethod
def deleted_forms() - > list
Return a list of forms that have been marked for deletion.
Returns
| Type | Description |
|---|
list | A list of Form instances where the deletion checkbox was checked |
@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
| Type | Description |
|---|
list | A 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
| Type | Description |
|---|
str | The default prefix string 'form' |
@classmethod
def get_deletion_widget() - > type
Returns the widget class used for the deletion checkbox.
Returns
| Type | Description |
|---|
type | The widget class for deletion fields |
@classmethod
def get_ordering_widget() - > type
Returns the widget class used for the ordering input.
Returns
| Type | Description |
|---|
type | The widget class for ordering fields |
@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
| Type | Description |
|---|
[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
| Type | Description |
|---|
list | A 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
| Type | Description |
|---|
int | The 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
| Type | Description |
|---|
bool | True 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
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
has_changed()
@classmethod
def has_changed() - > bool
Return True if data in any form differs from initial.
Returns
| Type | Description |
|---|
bool | True 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
| Name | Type | Description |
|---|
| form | [Form](../forms/form.md?sid=django_forms_forms_form) | The form instance to which fields should be added |
| index | int | The index of the form within the formset |
Returns
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
| Name | Type | Description |
|---|
| index | `int | str` |
Returns
| Type | Description |
|---|
str | A 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
| Type | Description |
|---|
bool | True if any form in the set requires a multipart encoding |
@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
| Type | Description |
|---|
[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
| Type | Description |
|---|
str | The path to the formset template |
get_context()
@classmethod
def get_context() - > dict
Returns the template context dictionary for rendering the formset.
Returns
| Type | Description |
|---|
dict | A dictionary containing the 'formset' instance |