serviceForModal=forms.ModelChoiceField(queryset=Services.objects.all(), required=False)
Which generates such select
<option value="6">nikita123</option>
I need ModelChoiceField to generate a value using text, in this case "nikia123", not id.
It is necessary to use ChoiceField
instead of ChoiceField
:
CHOICES = ((i.some_field, i.some_field) for i in Services.objects.all()) service_for_modal = forms.ChoiceField(choices=CHOICES, required=False)
You just have to be prepared for the fact that after validating the form, the field will not be a model instance, but the text from the value
attribute of the selected option
.
Source: https://ru.stackoverflow.com/questions/952496/
All Articles