How to remove 'selected' attribute from item in ModelChoiceField?
Question: ModelChoiceField adds attribute `selected` in HTML, if object from choices
list has FK to parent object.

How\where can I remove this 'selected' attribute in order to get just list of
choices? Want to mention, that I need to remove just 'selected' attribute, i.e
the value itself should not be removed from the list of choices. I need to
hook it somehow from python side, not from HTML. I tried to find needed
atribute in different places inside `form`, but no luck.

Does anyone know the part of Django code, where there is a check if an object
from choices list has FK to parent model?


Answer: 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](https://docs.djangoproject.com/en/1.10/_modules/django/forms/widgets/#Select),
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.

