Django 如何在ModelForm中创建RadioSelect非下颌和删除默认选项?
编辑我的问题与可能的重复问题不同的原因是,这并没有解决使其非重复的问题 manditory同时删除默认选项“-----” 我想在Django 如何在ModelForm中创建RadioSelect非下颌和删除默认选项?,django,django-models,django-forms,Django,Django Models,Django Forms,编辑我的问题与可能的重复问题不同的原因是,这并没有解决使其非重复的问题 manditory同时删除默认选项“-----” 我想在forms.ModelForm 要使用RadioSelect,我将在我的ModelForm(以下)中添加小部件,因为Djangos模型不提供RadioSelect或Select小部件 执行此操作的标准方法是将Blank=True作为参数传递。然而,当我在另一个问题中发现时,我问道 我必须删除默认选项“-----” 那么,如何使RadioSelect问题成为非强制性问题,
forms.ModelForm
要使用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,
}
#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?')
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,
}
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?')
型号.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,
}
#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?')
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,
}
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?')
考虑以下答案并使用RadioSelectNotNull
:
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,
}
#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?')
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,
}
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?')
请注意,如果您还可以将“选项”字段修改为:
型号.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,
}
#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?')
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,
}
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?')
值得深思的是,你创造选择的方式
互联网使用
并不理想。首先,元组具有相同的值,并且仍然定义了两次字符串,而且每个元组中的第一个元素是要在模型上设置的实际值,第二个元素是人类可读的名称。
谢谢,我一直在努力改进这一点。我不是100%确定你的意思,你能举一个一行的例子吗?当然,你可以这样想:internetusage=(“一”、“一天不到一小时”)、(“两”、“一天1-2小时”)、(“三”、“一天2-4小时”)、…
现在当你得到one的值时,它意味着第一个,Two
second选项已选中,依此类推。因此,使用此解决方案,我可以删除前几行,例如INTERNET\u少于一小时\u一天='少于一小时'
?这将大大减少我的代码。你能展示一下你添加的内容吗,那是不起作用的?因为它可以添加到任何地方,但只能确保,widgets={'internetu\u用法]:forms.RadioSelect,
更改为widgets={'internetu用法]:RadioSelectNotNull,
确保具有正确的导入语句