I’m building a form wizard. On the first step I have a form with one field. That field is a foreign key. Django’s default widget for a foreign key gives me the field’s options in a drop down (select) menu and represenets each option as an html option. I changed the default widget to a RadioSelect. So now it gives me the field’s options via radio buttons.
Here is my forms.py
class ChooseProductTypeForm(ModelForm):
class Meta:
model = Product
widgets = {
'type': RadioSelect,
}
What I would like to do is be able to loop through each option and get specific data for each option that I store on the model.
I was hoping it would be as simple as:
{% for field in form %}
{% for option in field %}
{{ option.image }}
{{ option.title }}
{{ option.description }}
{% endfor %}
{% endfor %}
After researching for a day and a half I’ve decided to see what the community has to say. FWIW, I’ve found some solutions that would work but are very complicated. There must be an easier way, no?
you can access the choices queryset, but note that a template is probably not the right place for this kind of logic. NB the
field.fieldwhich isn’t a typo