views:

21

answers:

2

This is my template tag in a forloop

{{ product.feature_set.all.1.value }}

i want to change the number 1 to the forloop.counter. is this posible?

like:

{{
product.feature_set.all.forloop.counter.value
}}

It does not work like that, but is there a way to do this?

A: 

This doesn't make sense. You should be looping through the queryset itself.

{% for feature in product.feature_set.all %}
    {{ feature }}
{% endfor %}
Daniel Roseman
no this is not what I want. I know you can do that. I want it like I asked. Is it possible or not?
Harry
+1  A: 

Since @Daniel's answer doesn't satisfy you, I thought you might want to try writing a custom filter. Here is a rough draft:

@register.filter
def custom_m2m(queryset, forloop_counter):
    return queryset[forloop_counter].value

You can use it in your template like this:

{% for ... %}
    {{ product.feature_set.all|custom_m2m:forloop.counter }}
{% endfor %}
Manoj Govindan