1👍
✅
I don’t know would it work or not, but an idea would be clear for you.
So, i found source of Select
widget that sets your selected
property in html. It’s here, just search for selected_html
.
You can try to subclass Select
widget:
from django.forms.widgets import Select
class CustomSelect(Select):
def render_option(self, selected_choices, option_value, option_label):
if option_value is None:
option_value = ''
option_value = force_text(option_value)
if option_value in selected_choices:
selected_html = '' # make it empty string like in else statement or refactor all that method
if not self.allow_multiple_selected:
# Only allow for a single selection.
selected_choices.remove(option_value)
else:
selected_html = ''
return format_html('<option value="{}"{}>{}</option>', option_value, selected_html, force_text(option_label))
And then in forms
class YourForm(forms.Form):
your_field = forms.ModelChoiceField(widget=CustomSelect())
...
It’s just solution that i came up with, and i know that this is not so elegant, but it seems that there is no simple way to disable that selected
thing.
Source:stackexchange.com