Skip to main content

label_for_field

Return a sensible label for a field name. The name can be a callable, property (but not created with @property decorator), or the name of an object's attribute, as well as a model field, including across related objects. If return_attr is True, also return the resolved attribute (which could be a callable). This will be None if (and only if) the name refers to a field.

def label_for_field(
name: string | callable,
model: django.db.models.Model,
model_admin: django.contrib.admin.ModelAdmin = null,
return_attr: boolean = false,
form: django.forms.BaseForm = null
) - > string | tuple

Return a sensible label for a field name. The name can be a callable, property (but not created with @property decorator), or the name of an object's attribute, as well as a model field, including across related objects. If return_attr is True, also return the resolved attribute (which could be a callable). This will be None if (and only if) the name refers to a field.

Parameters

NameTypeDescription
name`stringcallable`
modeldjango.db.models.ModelThe Django model class used as the starting point for field and attribute lookups.
model_admindjango.contrib.admin.ModelAdmin = nullAn optional ModelAdmin instance to check for attributes or short_description overrides.
return_attrboolean = falseIf True, the function returns a tuple of (label, resolved_attribute) instead of just the label.
formdjango.forms.BaseForm = nullAn optional form instance to check for field definitions if the name is not found on the model or admin.

Returns

TypeDescription
`stringtuple`