from django import template register = template.Library() @register.filter(name="human_key") def human_key_filter(obj, fieldname): """ Return the human key of a ``Choices`` field. Usage: ``{{ obj|human_key:"fieldname" }}`` Example: With ``Foo.STATUSES = Choices((1, "OPEN", "Open"), (2, "CLOSED", "Closed"))`` and ``Foo.status = ChoiceField(Foo.STATUSES)``. If ``foo.status = 1`` (i.e. ``Foo.STATUSES.OPEN``), returns ``Open``. """ return obj.get_human_key(fieldname)