Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining regroup with get_foo_display in Django templates

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.

like image 845
shacker Avatar asked Apr 30 '10 00:04

shacker


1 Answers

What happens if you do

{% regroup resources by get_restype_display as resource_list %}
like image 73
Daniel Roseman Avatar answered Nov 09 '22 00:11

Daniel Roseman