In a django form, how do I iterate over individual parameters in a field?

I create a form master. At the first stage, I have a form with one field. This field is a foreign key. The default widget for Django for a foreign key provides me with the field options in the Select menu and presents each option as an html parameter. I changed the default widget to RadioSelect. So now he gives me the field options with the switches.

Here are my forms.py

class ChooseProductTypeForm(ModelForm): class Meta: model = Product widgets = { 'type': RadioSelect, } 

What I would like to do is the ability to scroll through each option and get specific data for each option that I save in the model.

I was hoping it would be so simple:

 {% 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 decided to see what the community had to say. FWIW, I found some solutions that will work, but are very complex. There should be an easier way, no?

+4
source share
3 answers

you can access the query selection, but note that the template is probably not suitable for this type of logic. NB field.field which is not a typo

 {% for field in form %} {% for option in field.field.choices.queryset %} {{ option.image }} {{ option.title }} {{ option.description }} {% endfor %} {% endfor %} 
+7
source

Sorry, the fields are not repeated as you expect. What you are trying to do is not a collection of queries; this is a ModelChoiceField . You can access individual options, but they are represented internally as two sets with a foreign key identifier as the first element and __unicode__ as the second element.

As you said, there are ways to deal with this problem. The best way, albeit a little ugly, is probably to send a dictionary with the necessary information, using the model identifiers as keys. You can use the queryset value to do this quite easily.

0
source

I see that this is done in two ways.

You can create your own widget for this field, which will display the attributes of each model. It is very possible, but now I do not have time to write everything.

Another, faster method is to add a function to your form that handles this and returns the data you need.

 class ChooseProductTypeForm(ModelForm): def get_my_modelfield(self): for option in self.fields['my_modelfield'].queryset: yield { 'image': option.image, 'title': option.title, 'description': option.description } 

Basically, this causes the database to exit the template and simply use the easy-to-use form function to return the data you need. If the form field ever changes, you no longer scatter the corrective patterns, but simply update this function.

The disadvantage of this is that you cannot simply scroll through the fields in your form, you will need to display each of them explicitly. Of course, you can also play with __iter__ in your form, but that might be too hacky. Not sure if you are dealing entirely here :)

0
source

Source: https://habr.com/ru/post/1380927/


All Articles