简体   繁体   English

为什么选择字段在 django 查询集中显示键而不是值?

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

I have a Choice Field in my models.py我的models.py中有一个选择字段

models.py模型.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视图.py

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

template模板

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

output输出

closed_issue open_issue 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.谢谢你的帮助。

Of course there is a way:当然有办法:

{{ issue.get_status_display }}

In order to get the values of the STATUS es you must use a name convention get_<field_name>_display() .为了获取STATUS es 的值,您必须使用命名约定get_<field_name>_display() More on this here .更多关于这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM