Reputation: 701
I have a list of organized items that users may choose from using checkboxes. I am trying to pass a tuple of values for each checked checkbox so I can get information about the item itself and the group to which the item belongs. I have tried using hidden fields but it seems that the hidden field value is being passed regardless of if the corresponding checkbox has been checked or not.
If a checkbox is checked, I need the citation id and the parent software. Can I pass a tuple of (citation.id, sw) for each checked checkbox and, because multiple checkboxes can be checked, pass all of these together as a list of tuple? Like: [(citation1.id, sw1), (citation2.id, sw2), ] ? I need this info in my view.
Thank you for any help!
select_citations.html
{% for sw in software %}
{{sw}}
{% for citation in all_citations %}
<input type="checkbox" name="myselection[]" value="{{citation.id}}">
<input type="hidden" name="myselection[]" value="{{sw}}">
{% endfor %}
{% endfor %}
Upvotes: 0
Views: 750
Reputation: 45585
Compose IDs of both models to a single value for the checkbox:
{% for sw in software %}
{{sw}}
{% for citation in all_citations %}
<input type="checkbox" name="selection" value="{{citation.id}}-{{sw.id}}">
{% endfor %}
{% endfor %}
And then deconstruct this values in the view:
ids = [value.split('-') for value in request.POST.getlist('selection')]
Upvotes: 1