Jan Mejor
Jan Mejor

Reputation: 161

Django how to call a choice field in template

I have a model field with choices. Like this:

CHAIN_CHOICES = (
        ('P','Public'),
        ('N','Private')
        )
        chain = models.CharField(max_length=1, choices=CHAIN_CHOICES, verbose_name=u"Chain")

In my template I would like to call it simillary to this:

<li><i class="glyphicon glyphicon-tags"></i> <span>{{mymodel.chain}}</span></li>

Problem is .... this is calling 'N' or 'P' and I would like to call the 'Public' - 'Private' values. Any hint would be wellcome.

Regards,

J.M.

Upvotes: 0

Views: 1480

Answers (2)

Belloz
Belloz

Reputation: 423

@JanMejor
I want to call from templates to "choices" too but without duplicates. Here is a different way you did:

...
{% for i in mymodel %}
    {{ i.chain }}
{% endfor %}
...

Still, it will show duplicates if you have couple posts with the same "choice value"

Upvotes: 0

Jan Mejor
Jan Mejor

Reputation: 161

Guess I have found my answer:

{{ mymodel.get_chain_display }}

Unless there is a different way ??

Upvotes: 4

Related Questions