AdminField
This class acts as a wrapper for a form field within the administrative interface, managing its display properties and metadata. It determines the field's visual state, such as whether it is a checkbox, a readonly element, or the first item in a row. Additionally, it provides methods to generate specialized HTML label tags and error lists tailored for admin templates.
Attributes
| Attribute | Type | Description |
|---|---|---|
| field | django.forms.BoundField | The Django BoundField instance representing the specific form field being rendered. |
| is_first | boolean | A boolean flag indicating whether this field is the first element on its current display line. |
| is_checkbox | boolean | A boolean flag that identifies if the field's widget is a CheckboxInput, used to determine CSS classes and label positioning. |
| is_readonly | boolean = false | A boolean flag indicating whether the field is in a read-only state within the admin interface. |
| is_fieldset | boolean | A boolean flag derived from the widget's use_fieldset property that determines if the field should be wrapped in a fieldset/legend structure. |
Constructor
Signature
def AdminField(
form: django.forms.BaseForm,
field: str,
is_first: bool
)
Parameters
| Name | Type | Description |
|---|---|---|
| form | django.forms.BaseForm | The form instance containing the field. |
| field | str | The name of the field to be retrieved from the form. |
| is_first | bool | A flag indicating if this field is the first element on its display line. |
Methods
label_tag()
@classmethod
def label_tag() - > string
Generates the HTML label or legend tag for the field, applying specific CSS classes based on whether the field is required, a checkbox, or positioned inline.
Returns
| Type | Description |
|---|---|
string | The rendered HTML tag containing the field label and appropriate styling attributes. |
errors()
@classmethod
def errors() - > string
Returns the validation errors associated with the field formatted as an HTML unordered list.
Returns
| Type | Description |
|---|---|
string | A safe HTML string containing the list of field errors, or an empty string if no errors exist. |