如何使 RadioSelect 成为非强制性的并删除 ModelForm 中的默认选项?
How to make a RadioSelect non manditory and remove default option in a ModelForm?
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 "-------"
我想在forms.ModelForm
中做一个RadioSelect
调查问题非强制性
为了使用 RadioSelect
,我在我的 ModelForm
(下方)中添加了小部件,因为 Djangos 模型不提供 RadioSelect
或 Select
小部件。
执行此操作的标准方法是将 Blank=True
作为参数传递。然而,正如我在另一个问题中发现的那样,我问了
我必须删除默认选项“--------”。
那么如何使 RadioSelect 问题成为非强制性问题,同时不包括默认选项“------”?
谢谢
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?')
考虑 NickJ 的答案并使用 RadioSelectNotNull
如下:
forms.py
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)
class SurveyFormB(forms.ModelForm):
class Meta:
model = Person
fields = ['internet_usage', 'smart_phone_ownership', 'smart_phone_usage']
widgets = {'internet_usage' : RadioSelectNotNull,
'smart_phone_ownership' : forms.Select,
'smart_phone_usage' : RadioSelectNotNull,
}
请注意,如果您还可以将选择字段修改为:
models.py
INTERNET_USAGE = (
("One", 'Less than one hour a day'),
( "Two", '1 - 2 hours a day'),
( "Three", '2 - 4 hours a day'),
( "Four", '4 - 6 hours a Day'),
( "Five", '6 - 8 hours a day'),
( "Six", '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?')
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 "-------"
我想在forms.ModelForm
RadioSelect
调查问题非强制性
为了使用 RadioSelect
,我在我的 ModelForm
(下方)中添加了小部件,因为 Djangos 模型不提供 RadioSelect
或 Select
小部件。
执行此操作的标准方法是将 Blank=True
作为参数传递。然而,正如我在另一个问题中发现的那样,我问了
我必须删除默认选项“--------”。
那么如何使 RadioSelect 问题成为非强制性问题,同时不包括默认选项“------”?
谢谢
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?')
考虑 NickJ 的答案并使用 RadioSelectNotNull
如下:
forms.py
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)
class SurveyFormB(forms.ModelForm):
class Meta:
model = Person
fields = ['internet_usage', 'smart_phone_ownership', 'smart_phone_usage']
widgets = {'internet_usage' : RadioSelectNotNull,
'smart_phone_ownership' : forms.Select,
'smart_phone_usage' : RadioSelectNotNull,
}
请注意,如果您还可以将选择字段修改为:
models.py
INTERNET_USAGE = (
("One", 'Less than one hour a day'),
( "Two", '1 - 2 hours a day'),
( "Three", '2 - 4 hours a day'),
( "Four", '4 - 6 hours a Day'),
( "Five", '6 - 8 hours a day'),
( "Six", '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?')