Using a RadioSelect widget with ModelChoiceField

685 views
Skip to first unread message

Matt Rowbottom

unread,
Feb 5, 2021, 9:15:27 AM2/5/21
to Django users
Hello. I think I've found a bug.

Prior to Django v3.1, both of the fields in the following form would be rendered with an empty option, labelled as '---------':

```
from django import forms
from .models import Animal

class AnimalForm(forms.Form):
    animal_select = forms.ModelChoiceField(
        queryset=Animal.objects.all(),
        required=False,
    )
    animal_radio = forms.ModelChoiceField(
        queryset=Animal.objects.all(),
        required=False,
        widget=forms.RadioSelect()
    )
```

Since Django v3.1, the field using the RadioSelect widget no longer displays the empty option.

I've tested it with the following versions:

3.0    works
3.0.12    works
3.1    broken
3.1.6     broken
3.2a1    broken

I can't find anything in the docs or the v3.1 release notes (https://docs.djangoproject.com/en/dev/releases/3.1/#forms) to explain the change.

Does anyone know if this is intentional?

Siarhei Siarhei

unread,
Feb 5, 2021, 9:35:02 AM2/5/21
to Django users
to_field_name="name",

пятница, 5 февраля 2021 г. в 17:15:27 UTC+3, Matt Rowbottom:

Matt Rowbottom

unread,
Feb 5, 2021, 10:31:52 PM2/5/21
to Django users
Hi wne

Using `to_field_name` changes the value of the options/inputs, but doesn't seem to have any effect on whether a blank option is available. Using `required=False` should provide a blank option.

Below is a pic of how it's rendering. Animal Radio is missing a blank option.

Screen Shot 2021-02-06 at 2.25.47 pm.png

Siarhei Siarhei

unread,
Feb 6, 2021, 1:59:27 AM2/6/21
to Django users
https://stackoverflow.com/questions/14541074/empty-label-choicefield-django

суббота, 6 февраля 2021 г. в 06:31:52 UTC+3, Matt Rowbottom:

Matt Rowbottom

unread,
Feb 6, 2021, 4:01:18 AM2/6/21
to Django users
Hi wne

Thanks again for the suggestion, but those solutions are for how to make a `ChoiceField` behave like a `ModelChoiceField`.

My `ModelChoiceField` works fine while the default `Select` widget is used. But when I use the `RadioSelect` widget the blank option disappears. This only occurs in Django from version 3.1 onwards.

I'm not looking for workarounds - I would like to know why the RadioSelect isn't showing a blank option. If it's a bug, I will file a bug report.
Reply all
Reply to author
Forward
0 new messages