AdminReadonlyField
This class acts as a wrapper for read-only fields within an administrative interface, providing a consistent API for accessing field metadata and rendered content. It handles the retrieval of labels, help texts, and values from model instances while supporting specialized formatting for booleans, relationships, and HTML content. The class also facilitates the generation of administrative URLs for related objects and manages label tag rendering for form layouts.
Attributes
| Attribute | Type | Description |
|---|---|---|
| field | dict | A dictionary containing metadata about the field including its name, label, help text, original field object, and visibility status. |
| form | django.forms.Form | The Django form instance associated with this readonly field. |
| model_admin | django.contrib.admin.ModelAdmin | The ModelAdmin instance responsible for rendering the current model's admin interface. |
| is_first | bool | A boolean flag indicating if this is the first field in a row, used to determine CSS class application for labels. |
| is_checkbox | bool = false | A boolean flag indicating if the field should be rendered as a checkbox; always initialized to False for readonly fields. |
| is_readonly | bool = true | A boolean flag indicating that this field is read-only; always set to True. |
| empty_value_display | str | The string to display when the field's value is empty, retrieved from the ModelAdmin configuration. |
Constructor
Signature
def AdminReadonlyField(
form: object,
field: string|callable,
is_first: boolean,
model_admin: object = None
)
Parameters
| Name | Type | Description |
|---|---|---|
| form | object | The form instance associated with the field. |
| field | `string | callable` |
| is_first | boolean | A flag indicating if this is the first field in the set. |
| model_admin | object = None | The ModelAdmin instance associated with the field. |
Methods
label_tag()
@classmethod
def label_tag() - > string
Generates the HTML label tag for the readonly field, including the appropriate CSS classes and label suffix.
Returns
| Type | Description |
|---|---|
string | An HTML-safe string containing the formatted < label > element. |
get_admin_url()
@classmethod
def get_admin_url(
remote_field: [Field](../../../forms/fields/field.md?sid=django_forms_fields_field),
remote_obj: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model)
) - > string
Constructs an HTML anchor tag linking to the change page of a related object if a valid URL pattern is found.
Parameters
| Name | Type | Description |
|---|---|---|
| remote_field | [Field](../../../forms/fields/field.md?sid=django_forms_fields_field) | The field metadata object representing the relationship to the remote model. |
| remote_obj | [Model](../../../db/models/base/model.md?sid=django_db_models_base_model) | The specific model instance for which the admin change URL is being generated. |
Returns
| Type | Description |
|---|---|
string | An HTML-safe string containing the link to the related object, or the string representation of the object if no URL matches. |
contents()
@classmethod
def contents() - > string
Retrieves and formats the value of the readonly field for display, handling booleans, relationships, and HTML escaping.
Returns
| Type | Description |
|---|---|
string | The formatted value of the field, which may include HTML icons for booleans or links for foreign keys. |