Combining regroup with get_foo_display in Django templates
Posted
by shacker
on Stack Overflow
See other posts from Stack Overflow
or by shacker
Published on 2010-04-30T00:38:39Z
Indexed on
2010/04/30
0:47 UTC
Read the original article
Hit count: 281
I'm using the regroup template tag to group queryset output on a Choices field. In the model:
RESOURCE_TYPES = (
('tut','External tutorial'),
('read','Additional reading'),
('org','Company or organization'),
)
restype = models.CharField('Resource type',max_length=6,choices=RESOURCE_TYPES)
in the view:
resources = Resource.objects.filter(tutorial=tutorial)
in the template:
{% regroup resources by restype as resource_list %}
{% for type in resource_list %}
<h3>{{type.grouper}}</h3>
So type.grouper renders as 'tut' or 'org' on the page, rather than the long form. Normally you would use the get_foo_display syntax to get at the value of the choice, rather than the key. But the value doesn't seem to be available after going through regroup. There's no way I can find to use get_foo_display on {{type.grouper}}.
It makes sense when you think about it, but what's the workaround? Thanks.
© Stack Overflow or respective owner