Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

field's verbose_name in templates

Suppose I have a model:

from django.db import models

class Test(models.Model):
    name=models.CharField(max_length=255, verbose_name=u'custom name')

How do I get my model's field's verbose name in templates? The following doesn't work:

{{ test_instance.name.verbose_name }}

I would very much appreciate the solution, something on lines as we do when using forms, using label attribute in template:

{{ form_field.label }}
like image 300
Delremm Avatar asked Jan 24 '13 08:01

Delremm


3 Answers

You can use following python code for this

Test._meta.get_field("name").verbose_name.title()

If you want to use this in template then it will be best to register template tag for this. Create a templatetags folder inside your app containing two files (__init__.py and verbose_names.py).Put following code in verbose_names.py:

from django import template
register = template.Library()

@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()

Now you can use this template tag in your template after loading the library like this:

{% load verbose_names %}
{% get_verbose_field_name test_instance "name" %}

You can read about Custom template tags in official django documentation.

like image 150
pankaj28843 Avatar answered Nov 17 '22 04:11

pankaj28843


The method in the accepted answer is awesome!

And maybe you'll like this if you want to generate a field list.

Adding an iterable to the class Test makes it convenient to list fields' verbose name and value.

Model

class Test(models.Model):
...
def __iter__(self):
    for field in self._meta.fields:
        yield (field.verbose_name, field.value_to_string(self))

Template

{% for field, val in test_instance %}
<div>
    <label>{{ field }}:</label>
    <p>{{ val }}</p>
</div>
{% endfor %}
like image 44
misssprite Avatar answered Nov 17 '22 03:11

misssprite


based on this answer https://stackoverflow.com/a/14498938 .in Django Model i added

class Meta:
  app_name = 'myapp'

in listview i have

from django.core import serializers
context['data'] = serializers.serialize( "python", self.get_queryset() )

inside mylist.html i have

{% for field, value in data.0.fields.items %}
<th style="text-align:center;">{% get_verbose_field_name data.0.model field %}</th>
{% endfor %}

in filter:

from django import template
register = template.Library()
from .models import Mymodel
@register.simple_tag
def get_verbose_field_name(instance, field_name):
  """
  Returns verbose_name for a field.
  """
  myinstance = eval(instance.split('.')[1].title())
  return myinstance._meta.get_field(field_name).verbose_name.title()

instance in the abbove filter for the specific example is myapp.mymodel i evalute instance into model object and the i return field verbose name

it works in django 1.9

like image 1
Dimitris Kougioumtzis Avatar answered Nov 17 '22 05:11

Dimitris Kougioumtzis