EDIT The reason my question is different to the possible duplicate is because that does not address the issue of making it non manditory while also removing the default option "-------"
I want to make a RadioSelect
survey question non mandatory in a forms.ModelForm
To use RadioSelect
I am adding the widget in my ModelForm
(below) as Djangos model does not provide a RadioSelect
or Select
widget.
The standard way of doing this is to pass Blank=True
as an argument. However as I found out in another question I asked it turns out that if you pass blank=True
as an argument from models.CharField
to the forms.RadioSelect
widget it will leave the default option "-------" in place even if you use default=None
.
I have to remove the default option "-------".
So how do I make the RadioSelect questions non mandatory while also not including the default option "-------"?
Thanks
forms.py
class SurveyFormB(forms.ModelForm):
class Meta:
model = Person
fields = ['internet_usage', 'smart_phone_ownership', 'smart_phone_usage']
widgets = {'internet_usage' : forms.RadioSelect,
'smart_phone_ownership' : forms.Select,
'smart_phone_usage' : forms.RadioSelect,
}
models.py
#How often do you use the Internet?
INTERNET_LESS_THAN_ONE_HOUR_A_DAY = 'Less than one hour per day'
INTERNET_ONE_TO_TWO_HOURS_A_DAY = '1 - 2 hours per day'
INTERNET_TWO_TO_FOUR_HOURS_A_DAY = '2 - 4 hours per day'
INTERNET_FOUR_TO_SIX_HOURS_A_DAY = '4 - 6 hours per day'
INTERNET_SIX_TO_EIGHT_HOURS_A_DAY = '6 - 8 hours per day'
INTERNET_EIGHT_PLUS_HOURS_A_DAY = '8 + hours per day'
INTERNET_USAGE = (
(INTERNET_LESS_THAN_ONE_HOUR_A_DAY, 'Less than one hour a day'),
(INTERNET_ONE_TO_TWO_HOURS_A_DAY, '1 - 2 hours a day'),
(INTERNET_TWO_TO_FOUR_HOURS_A_DAY, '2 - 4 hours a day'),
(INTERNET_FOUR_TO_SIX_HOURS_A_DAY, '4 - 6 hours a Day'),
(INTERNET_SIX_TO_EIGHT_HOURS_A_DAY, '6 - 8 hours a day'),
(INTERNET_EIGHT_PLUS_HOURS_A_DAY, '8 + hours a day'),
)
internet_usage = models.CharField(null=True, max_length=100, default=None, choices=INTERNET_USAGE, verbose_name='How long do you spend on the Internet each day?')