[Solved]-How to get rid of the bogus choice generated by RadioSelect of Django Form

9πŸ‘

βœ…

Even without blank=True it shows the extra input. I have created a new Widget:

from itertools import chain
from django.forms import RadioSelect
from django.utils.encoding import force_unicode

class RadioSelectNotNull(RadioSelect):
    def get_renderer(self, name, value, attrs=None, choices=()):
        """Returns an instance of the renderer."""
        if value is None: value = ''
        str_value = force_unicode(value) # Normalize to string.
        final_attrs = self.build_attrs(attrs)
        choices = list(chain(self.choices, choices))
        if choices[0][0] == '':
            choices.pop(0)
        return self.renderer(name, str_value, final_attrs, choices)
πŸ‘€NickJ

6πŸ‘

Set blank=False (or just remove it) and also add default='Unspecified'

πŸ‘€frnhr

1πŸ‘

You can set the choices when you set the widget. It’s showing the β€”- because in your model you have blank=True.

Just use the choices arg of the widget and set it to the choices you set in your model.

πŸ‘€dting

1πŸ‘

By default the widget used by ModelChoiceField will have an empty choice at the top of the list.

You can change the text of this label (which is β€œβ€”β€”β€”β€ by default) with the empty_label attribute, or you can disable the empty label entirely by setting empty_label to None:

A custom empty label:

field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")

No empty label:

field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
πŸ‘€violenttom

0πŸ‘

Django <= 1.10

RadioSelectNotNull widget

from itertools import chain
from django.forms import RadioSelect
from django.utils.encoding import force_unicode

class RadioSelectNotNull(RadioSelect):
"""
A widget which removes the default '-----' option from RadioSelect
"""
    def get_renderer(self, name, value, attrs=None, choices=()):
        """Returns an instance of the renderer."""
        if value is None: value = ''
        str_value = force_unicode(value) # Normalize to string.
        final_attrs = self.build_attrs(attrs)
        choices = list(chain(self.choices, choices))
        if choices[0][0] == '':
            choices.pop(0)
        return self.renderer(name, str_value, final_attrs, choices)

Django >= 1.11

As after Django 1.10 following method is no more in RadioSelect or in its ancestors. That’s why upper widget will not remove bogus choice generated by RadioSelect.

def get_renderer(self, name, value, attrs=None, choices=()):

So to remove bogus choice generated by RadioSelect use following widget. I have tested this till Django 2.0

from django.forms import RadioSelect
class RadioSelectNotNull(RadioSelect):
    """
    A widget which removes the default '-----' option from RadioSelect
    """
    def optgroups(self, name, value, attrs=None):
        """Return a list of optgroups for this widget."""
        if self.choices[0][0] == '':
            self.choices.pop(0)
        return super(RadioSelectNotNull, self).optgroups(name, value, attrs)

Leave a comment