Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterate choices and get_FOO_display #120

Open
jucajuca opened this issue Dec 2, 2020 · 2 comments
Open

iterate choices and get_FOO_display #120

jucajuca opened this issue Dec 2, 2020 · 2 comments

Comments

@jucajuca
Copy link

jucajuca commented Dec 2, 2020

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!

@jucajuca
Copy link
Author

jucajuca commented Dec 3, 2020

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 %}

@spapas
Copy link

spapas commented Mar 31, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants