How can I create a grouped set of elements for Grouped Radio Buttons

How can I create a set of elements to place in views?

GROUPED_CHOICES

I have a list of elements.

zzz = [m1-x, m2-x] - the number of repetitions of each group name - the number of elements to group xxx = [m1,m1, m1, m1, m2, m2 ] - all group names that must belong to the choices elements. sss = [q, w, e, r, t, y] - the choices elements

vvv = [('q', 'q), ('w', 'w'), ('e', 'e'),('r', 'r), ('t', 't'), ('y', 'y')]

[ -- m1-- {{{ ('q', 'q), ('w', 'w'), ('e', 'e'),('r', 'r) }}} , --m2-- {{{ ('t', 't'), ('y', 'y') }}} ] This is roughly how I would like to group

but the lists will always be different - dynamic.

I took this from chatgpt. but I don't know yet how to create such a set of elements.

GROUPED_CHOICES = [
    ('Fruits', [
        ('apple', 'Apple'),
        ('banana', 'Banana'),
    ]),
    ('Vegetables', [
        ('carrot', 'Carrot'),
        ('lettuce', 'Lettuce'),
    ]), ]

from django import forms

class FoodForm(forms.Form):
    food = forms.ChoiceField(
        choices=GROUPED_CHOICES,
        widget=forms.RadioSelect
    )

{% for group_label, group_choices in form.food.field.choices %}
    <fieldset>
        <legend>{{ group_label }}</legend>
        {% for choice_value, choice_label in group_choices %}
            {# Get the radio input manually using the field's id_for_label logic #}
            {% with field_id=form.food.auto_id %}
                {% set widget_id = field_id|string + '_' + choice_value %}
            {% endwith %}
            <label for="{{ widget_id }}">
                <input type="radio" name="{{ form.food.name }}"
                       value="{{ choice_value }}"
                       id="{{ widget_id }}"
                       {% if form.food.value == choice_value %}checked{% endif %}>
                {{ choice_label }}
            </label><br>
        {% endfor %}
    </fieldset>
{% endfor %}

You havent specified the format of your input clearly. I'm assuming you have the lists 'zzz' and 'vvv' already as input. Since for zzz= [m1-4, m2-2] it is unclear what format it is in, I'll make it into 2 separate lists.

GROUPED_choices = []

group_names = ['m1','m2']
repetitions = [4,2]
vvv = [('q', 'q'), ('w', 'w'), ('e', 'e'),('r', 'r'), ('t', 't'), ('y', 'y')]

for index in range(len(repetitions)):
    choice_list = []
    counter = 0
    while counter<repetitions[index]:
        choice_list.append(vvv[counter])
        counter+=1
    GROUPED_choices.append((group_names[index], choice_list))

print(GROUPED_choices)

This should give you an output of [('m1', [('q', 'q'), ('w', 'w'), ('e', 'e'), ('r', 'r')]), ('m2', [('q', 'q'), ('w', 'w')])]

Use this to build GROUPED_CHOICES dynamically:

```python
from collections import defaultdict

xxx = ['m1', 'm1', 'm1', 'm1', 'm2', 'm2']
sss = ['q', 'w', 'e', 'r', 't', 'y']
vvv = list(zip(sss, sss))

grouped = defaultdict(list)
for g, c in zip(xxx, vvv):
    grouped[g].append(c)

GROUPED_CHOICES = list(grouped.items())
```

Example output:

python     [         ('m1', [('q', 'q'), ('w', 'w'), ('e', 'e'), ('r', 'r')]),         ('m2', [('t', 't'), ('y', 'y')])     ]     

Use in Django form:

python     forms.ChoiceField(choices=GROUPED_CHOICES)     

CMIIW

Вернуться на верх