goinnn/django-multiselectfield

iterate choices and get_FOO_display

jucajuca opened this issue · 2 comments

Hello,
How can I iterate at the same time the values and the display strings for a choice?
I tried this and some other options, but I can't figure it out.

        {% for choice in multifield %}
          {{choice }} -- {{ choice.get_display }}.
        {% endfor %}

any help is appreciated. thanks!

I had to do it like this:

tags.py
@register.filter
def get_item(dictionary, key):
return the dictionary.get(key)

    {% for key in multifield %}
      {{ key }} -- {{ multifield.choices | get_item:key }}.
    {% endfor %}

but it would be better to loop through the selected choices as usual with the usual:

    {% for key, value in multifield %}
      {{ key }} -- {{ value }}.
    {% endfor %}

Hey @goinnn could you give some love to this issue? Maybe we can add another method like get_FOO_display to class so it returns the list of key-value as proposed here? I'd be happy to provide a PR with the functionality if you like.

Kind regards,
Serafeim