pythondjangochoicefield

Why choice field displays keys instead of values in django queryset?


I have a Choice Field in my models.py

models.py

STATUS = (
    ('closed_issue', 'Closed Issue'),
    ('open_ssue', 'Open Issue'),
    ('pending', 'Pending'),
)

class Issue(models.Model):
    name = models.CharField(max_length=45)
    status = models.CharField(max_length=50, choices=STATUS)

views.py

def Issues(resuest):
    issues = Issue.objects.all()

template

{% for issue in issues %}
     {{ issue.status }}
{% endfor %}

Output

closed_issue 
open_issue 

It displays the keys of the choice field instead of values

I want the values to be displayed in the template. Is there a way to get the values instead of keys?

Thanks for any help.


Solution

  • Of course there is a way:

    {{ issue.get_status_display }}

    In order to get the values of the STATUSes you must use a name convention get_<field_name>_display(). More on this here.