【发布时间】:2010-04-30 00:38:39
【问题描述】:
我正在使用regroup 模板标签对Choices 字段上的查询集输出进行分组。在模型中:
RESOURCE_TYPES = (
('tut','External tutorial'),
('read','Additional reading'),
('org','Company or organization'),
)
restype = models.CharField('Resource type',max_length=6,choices=RESOURCE_TYPES)
在视图中:
resources = Resource.objects.filter(tutorial=tutorial)
在模板中:
{% regroup resources by restype as resource_list %}
{% for type in resource_list %}
<h3>{{type.grouper}}</h3>
所以 type.grouper 在页面上呈现为“tut”或“org”,而不是长格式。通常你会使用get_foo_display 语法来获取选择的值,而不是键。但是经过重组后,该值似乎不可用。我找不到在 {{type.grouper}} 上使用 get_foo_display 的方法。
仔细想想是有道理的,但解决方法是什么?谢谢。
【问题讨论】: