Home > Software design >  Prevent capitalization in model field's verbose_name
Prevent capitalization in model field's verbose_name

Time:08-17

I use the following template tag:

@register.simple_tag
def get_verbose_field_name(instance, field_name):
    """
    Returns verbose_name for a field.
    """
    return instance._meta.get_field(field_name).verbose_name.title()

Assume I have defined a model field lorem_ipsum with the field's verbose_name="FOO bar".

Then, when I run {% get_verbose_field_name object "lorem_ipsum" %} in the template in a table's <th> element, I receive "Foo Bar".

However, I want to keep the verbose name exactly how I defined it—in this example as "FOO bar". How can one disable auto-capitalization of verbose names?

CodePudding user response:

You can use both of the below methods for this purpose:

@register.simple_tag
def get_verbose_field_name(instance, field_name):
    """
    Returns verbose_name for a field.
    """
    return instance._meta.get_field(field_name).verbose_name

or

@register.simple_tag
def get_verbose_field_name(instance, field_name):
    """
    Returns verbose_name for a field.
    """
    return instance._meta.get_field_by_name(field_name).verbose_name

CodePudding user response:

Remove the call to title():

@register.simple_tag
def get_verbose_field_name(instance, field_name):
    """
    Returns verbose_name for a field.
    """
    return instance._meta.get_field(field_name).verbose_name
  • Related