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
| Attribute | Type | Description |
|---|---|---|
| form | django.forms.Form | The Django form instance associated with this fieldset used to retrieve field data and errors. |
| name | string | The title or label of the fieldset displayed to the user. |
| fields | tuple | A collection of field names or field lines to be rendered within this fieldset. |
| classes | string | A space-separated string of CSS classes applied to the fieldset container for styling and behavior. |
| description | string | Optional explanatory text displayed at the top of the fieldset. |
| model_admin | django.contrib.admin.ModelAdmin | The ModelAdmin instance providing context for field rendering and permissions. |
| readonly_fields | tuple | A collection of field names that should be rendered as non-editable text. |
| media | django.forms.Media | Returns the form media assets required for the fields within this fieldset. |
| is_collapsible | boolean | Indicates 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
| Name | Type | Description |
|---|---|---|
| form | django.forms.BaseForm | The form instance containing the fields for this fieldset. |
| name | str = None | The title or name of the fieldset. |
| readonly_fields | tuple = () | A collection of field names that should be rendered as read-only. |
| fields | tuple = () | A collection of field names to be included in this fieldset. |
| classes | tuple = () | A collection of CSS classes to apply to the fieldset container. |
| description | str = None | Optional descriptive text to display at the top of the fieldset. |
| model_admin | django.contrib.admin.ModelAdmin = None | The ModelAdmin instance associated with the fieldset. |
Methods
media()
@classmethod
def media() - > forms.Media
Provides the media assets required for rendering the fieldset components.
Returns
| Type | Description |
|---|---|
forms.Media | A 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
| Type | Description |
|---|---|
boolean | True if the 'collapse' class is present and no fields within the fieldset contain errors; otherwise False. |