Skip to main content

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

AttributeTypeDescription
fielddictA dictionary containing metadata about the field including its name, label, help text, original field object, and visibility status.
formdjango.forms.FormThe Django form instance associated with this readonly field.
model_admindjango.contrib.admin.ModelAdminThe ModelAdmin instance responsible for rendering the current model's admin interface.
is_firstboolA boolean flag indicating if this is the first field in a row, used to determine CSS class application for labels.
is_checkboxbool = falseA boolean flag indicating if the field should be rendered as a checkbox; always initialized to False for readonly fields.
is_readonlybool = trueA boolean flag indicating that this field is read-only; always set to True.
empty_value_displaystrThe 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

NameTypeDescription
formobjectThe form instance associated with the field.
field`stringcallable`
is_firstbooleanA flag indicating if this is the first field in the set.
model_adminobject = NoneThe 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

TypeDescription
stringAn 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

NameTypeDescription
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

TypeDescription
stringAn 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

TypeDescription
stringThe formatted value of the field, which may include HTML icons for booleans or links for foreign keys.