Skip to main content

Fieldset

This class represents a logical grouping of form fields, providing a structured way to manage field layout, descriptions, and CSS classes. It supports features like collapsible sections based on form errors and handles the iteration of fields into individual field lines. The class also integrates with model administration components to manage read-only fields and associated media.

Attributes

AttributeTypeDescription
formdjango.forms.FormThe Django form instance associated with this fieldset used to retrieve field data and errors.
namestringThe title or label of the fieldset displayed to the user.
fieldstupleA collection of field names or field lines to be rendered within this fieldset.
classesstringA space-separated string of CSS classes applied to the fieldset container for styling and behavior.
descriptionstringOptional explanatory text displayed at the top of the fieldset.
model_admindjango.contrib.admin.ModelAdminThe ModelAdmin instance providing context for field rendering and permissions.
readonly_fieldstupleA collection of field names that should be rendered as non-editable text.
mediadjango.forms.MediaReturns the form media assets required for the fields within this fieldset.
is_collapsiblebooleanIndicates if the fieldset can be collapsed, which is true if 'collapse' is in classes and no fields contain errors.

Constructor

Signature

def Fieldset(
form: django.forms.BaseForm,
name: str = None,
readonly_fields: tuple = (),
fields: tuple = (),
classes: tuple = (),
description: str = None,
model_admin: django.contrib.admin.ModelAdmin = None
) - > null

Parameters

NameTypeDescription
formdjango.forms.BaseFormThe form instance containing the fields for this fieldset.
namestr = NoneThe title or name of the fieldset.
readonly_fieldstuple = ()A collection of field names that should be rendered as read-only.
fieldstuple = ()A collection of field names to be included in this fieldset.
classestuple = ()A collection of CSS classes to apply to the fieldset container.
descriptionstr = NoneOptional descriptive text to display at the top of the fieldset.
model_admindjango.contrib.admin.ModelAdmin = NoneThe ModelAdmin instance associated with the fieldset.

Methods


media()

@classmethod
def media() - > forms.Media

Provides the media assets required for rendering the fieldset components.

Returns

TypeDescription
forms.MediaA Media object containing the CSS and JavaScript assets needed for the fieldset's display and interaction.

is_collapsible()

@classmethod
def is_collapsible() - > boolean

Determines if the fieldset can be collapsed based on its CSS classes and the presence of validation errors.

Returns

TypeDescription
booleanTrue if the 'collapse' class is present and no fields within the fieldset contain errors; otherwise False.