django重新组合石斑鱼名称作为选择详细名称

时间:2013-01-03 18:01:31

标签: python django django-models django-templates

从Django文档中获取简单的重组示例:

{% regroup cities by country as country_list %}

<ul>
{% for country in country_list %}
    <li>{{ country.grouper }}
    <ul>
        {% for item in country.list %}
          <li>{{ item.name }}: {{ item.population }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}

如果country.grouper是在模型中使用choices = CHOICES_FIELD声明的Char字段,如何在模板中显示其详细名称?通常我会采用Model.get_FOO_display,但country.grouper.get_country_list_display当然不会起作用。
是自定义模板标签吗?

1 个答案:

答案 0 :(得分:7)

你只需要反过来看看它!

使用get_FOO_display作为分组字段。

  

{%by get_country_display将city重新组合为country_list%}

     

{{country.grouper}}现在将显示来自的值字段   选择设置而不是键。

(从djangodocs逐字逐句)