Tuxedo Joe Tuxedo Joe - 1 year ago 105
Python Question

Django form error: "Select a valid choice" on RadioSelect()

Trying to figure out what's going wrong here. I have a ModelForm where I need a radio select between three colors. I get the following error:

"Select a valid choice. This is not one of the available choices"

models.py:

COLORS = (
('1', 'Röd'),
('2', 'Gul'),
('3', 'Blå'),)

class Poster(models.Model):
title = models.CharField(max_length=100)
colors = models.IntegerField(choices=COLORS, default=2)


forms.py:

class PosterForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PosterForm, self).__init__(*args, **kwargs)

class Meta:
model = Poster

fields = ('title', 'colors')

labels = {
"title": "Rubrik",
"colors": "Färg",
}

widgets = {
'colors': forms.RadioSelect(attrs={'choices': "[(1, 'Röd'), (2, 'Gul'),(3, 'Blå')]"}),
}


template.html:

<div id="id_colors">
<div class="radio"><label for="id_colors_0"><input class="" id="id_colors_0" name="colors" title="" type="radio" value="1" required /> Röd</label></div>
<div class="radio"><label for="id_colors_1"><input checked="checked" class="" id="id_colors_1" name="colors" title="" type="radio" value="2" required /> Gul</label></div>
<div class="radio"><label for="id_colors_2"><input class="" id="id_colors_2" name="colors" title="" type="radio" value="3" required /> Blå</label></div>
</div>


{% if form.colors.errors %}
<div class="alert alert-danger">
<strong>{{ form.colors.errors|escape }}</strong>
</div>
{% endif %}


Happy for any help!

Answer Source

Turns out IntegerField isn't so keen on a number value inside a String. Changing this approach to use letters and a CharField did the trick.

models.py:

COLORS = (
    ('r', 'Röd'),
    ('y', 'Gul'),
    ('b', 'Blå'),)

class Poster(models.Model):
    title = models.CharField(max_length=100)
    colors = models.CharField(choices=COLORS, max_length=1)

forms.py:

class PosterForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(PosterForm, self).__init__(*args, **kwargs)

    class Meta:
        model = Poster

        fields = ('title', 'colors')

        labels = {
                "title": "Rubrik",
                "colors": "Färg",
        }

        widgets = {
           *'colors': forms.RadioSelect(),*
    }

template.html:

<div id="id_colors">
    <div class="radio"><label for="id_colors_0"><input class="" id="id_colors_0" name="colors" title="" type="radio" value="r" required /> Röd</label></div>
    <div class="radio"><label for="id_colors_1"><input checked="checked" class="" id="id_colors_1" name="colors" title="" type="radio" value="y" required /> Gul</label></div>
    <div class="radio"><label for="id_colors_2"><input class="" id="id_colors_2" name="colors" title="" type="radio" value="b" required /> Blå</label></div>
</div>


{% if form.colors.errors %}
    <div class="alert alert-danger">
        <strong>{{ form.colors.errors|escape }}</strong>
    </div>
{% endif %}

Thanks Cheng for this post which helped me: http://cheng.logdown.com/posts/2015/05/25/django-create-a-radio-input-using-bootstrap3s-inline-style

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download