AdminForm
This class acts as a wrapper for a form and its associated fieldsets to facilitate rendering within an administration interface. It manages prepopulated fields, read-only fields, and fieldset organization while providing access to form errors and media. The class is designed to be iterable, yielding individual fieldset objects for structured display.
Attributes
| Attribute | Type | Description |
|---|---|---|
| form | django.forms.Form | The Django form instance used to manage data validation and field rendering. |
| fieldsets | iterable | An iterable of fieldset configurations defining the grouping and layout of form fields. |
| prepopulated_fields | list | A list of dictionaries mapping form fields to their dependent fields for automatic value generation. |
| model_admin | django.contrib.admin.ModelAdmin | The ModelAdmin instance associated with this form, providing administrative context and logic. |
| readonly_fields | tuple = () | A collection of field names that should be rendered as non-editable text within the form. |
| errors | django.forms.utils.ErrorDict | Returns the collection of validation errors associated with the underlying form. |
| non_field_errors | django.forms.utils.ErrorList | Returns errors from the underlying form that are not associated with a specific field. |
| fields | dict | Provides access to the dictionary of form fields defined in the underlying form instance. |
| is_bound | boolean | Indicates whether the underlying form has data bound to it for validation. |
| media | django.forms.widgets.Media | The combined media definitions for the form and all associated fieldsets, including CSS and JavaScript. |
Constructor
Signature
def AdminForm(
form: django.forms.Form,
fieldsets: list,
prepopulated_fields: dict,
readonly_fields: list = None,
model_admin: django.contrib.admin.ModelAdmin = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| form | django.forms.Form | The form instance to be rendered. |
| fieldsets | list | A list of fieldsets defining the layout of the form. |
| prepopulated_fields | dict | A dictionary mapping field names to their dependencies for automatic population. |
| readonly_fields | list = None | A list of fields that should be rendered as read-only. |
| model_admin | django.contrib.admin.ModelAdmin = None | The ModelAdmin instance associated with the form. |
Methods
errors()
@classmethod
def errors() - > [ErrorDict](../../../forms/utils/errordict.md?sid=django_forms_utils_errordict)
Accesses the collection of validation errors associated with the underlying form instance.
Returns
| Type | Description |
|---|---|
[ErrorDict](../../../forms/utils/errordict.md?sid=django_forms_utils_errordict) | A dictionary-like object mapping field names to their respective list of error messages. |
non_field_errors()
@classmethod
def non_field_errors() - > [ErrorList](../../../forms/utils/errorlist.md?sid=django_forms_utils_errorlist)
Retrieves errors that are not specific to a particular field, such as those originating from the form's clean() method.
Returns
| Type | Description |
|---|---|
[ErrorList](../../../forms/utils/errorlist.md?sid=django_forms_utils_errorlist) | A list of error messages that apply to the form as a whole. |
fields()
@classmethod
def fields() - > dict
Provides access to the dictionary of bound or unbound field objects defined on the underlying form.
Returns
| Type | Description |
|---|---|
dict | A dictionary mapping field names to form field objects. |
is_bound()
@classmethod
def is_bound() - > boolean
Indicates whether the underlying form has been provided with data to validate.
Returns
| Type | Description |
|---|---|
boolean | True if the form has data or files attached, False otherwise. |
media()
@classmethod
def media() - > [Media](../../../forms/widgets/media.md?sid=django_forms_widgets_media)
Aggregates the required JavaScript and CSS assets from both the underlying form and all individual fieldsets.
Returns
| Type | Description |
|---|---|
[Media](../../../forms/widgets/media.md?sid=django_forms_widgets_media) | A Media object containing the combined static file paths for the form and its fieldsets. |