gpt4 book ai didi

django - 如何在 ModelForm 中指定 Select 和 RadioSelect?

转载 作者:行者123 更新时间:2023-12-05 08:57:41 28 4
gpt4 key购买 nike

我正在将调查从 Form 转换为 Django 1.6.2 中的 ModelForm

谁能告诉我 forms.ChoiceField(widget=forms.Select(),forms.ChoiceField(widget=forms.RadioSelect() 是什么意思使用 ModelForm?

我试过 widget=models.Select()widget=models.RadioSelect() 但它一直报错

AttributeError: 'module' object has no attribute 'Select'

AttributeError: 'module' object has no attribute 'RadioSelect'

旧代码

表单.py

class SurveyFormB(forms.Form): 

#Do you own a Smartphone?
YES_SMARTPHONE = 'Yes'
NO_SMARTPHONE = 'No'

SMART_PHONE_OWNERSHIP = (
(YES_SMARTPHONE, 'Yes'),
(NO_SMARTPHONE, 'No'),
)
smart_phone_ownership = forms.ChoiceField(widget=forms.RadioSelect(), choices=SMART_PHONE_OWNERSHIP, initial= "", label='Do you own a Smartphone?', required = False)


#If 'Yes' How many hours a day do you access the Internet on it?
SMART_PHONE_LESS_THAN_ONE_HOUR_A_DAY = 'Less than one hour day'
SMART_PHONE_ONE_TO_TWO_HOURS_A_DAY = '1 - 2 Hours a day'
SMART_PHONE_TWO_TO_FOUR_HOURS_A_DAY = '2 - 4 hours a day'
SMART_PHONE_FOUR_TO_SIX_HOURS_A_DAY = '4 - 6 hours a day'
SMART_PHONE_SIX_TO_EIGHT_HOURS_A_DAY = '6 - 8 hours a day'
SMART_PHONE_EIGHT_PLUS_HOURS_A_DAY = '8 + hours a day'


SMART_PHONE_USAGE = (
("", "----------"),
(SMART_PHONE_LESS_THAN_ONE_HOUR_A_DAY, 'Less than one hour a day'),
(SMART_PHONE_ONE_TO_TWO_HOURS_A_DAY, '1 - 2 Hours a day'),
(SMART_PHONE_TWO_TO_FOUR_HOURS_A_DAY, '2 - 4 hours a day'),
(SMART_PHONE_FOUR_TO_SIX_HOURS_A_DAY, '4 - 6 hours a Day'),
(SMART_PHONE_SIX_TO_EIGHT_HOURS_A_DAY, '6 - 8 hours a day'),
(SMART_PHONE_EIGHT_PLUS_HOURS_A_DAY, '8 + hours a day'),
)

smart_phone_usage = forms.ChoiceField(widget=forms.Select(), choices=SMART_PHONE_USAGE, initial= "", label='If Yes, How many hours a day do you access the Internet on it?', required = False)

新代码(不工作)

modules.py

#Do you own a Smartphone?   
YES_SMARTPHONE = 'Yes'
NO_SMARTPHONE = 'No'


SMART_PHONE_OWNERSHIP = (
(YES_SMARTPHONE, 'Yes'),
(NO_SMARTPHONE, 'No'),
)
smart_phone_ownership = models.CharField(null=True, max_length=1, widget=models.RadioSelect(), choices=SMART_PHONE_OWNERSHIP, verbose_name='Do you own a Smartphone?')



#If 'Yes' How many hours a day do you access the Internet on it?
SMART_PHONE_LESS_THAN_ONE_HOUR_A_DAY = 'Less than one hour day'
SMART_PHONE_ONE_TO_TWO_HOURS_A_DAY = '1 - 2 Hours a day'
SMART_PHONE_TWO_TO_FOUR_HOURS_A_DAY = '2 - 4 hours a day'
SMART_PHONE_FOUR_TO_SIX_HOURS_A_DAY = '4 - 6 hours a day'
SMART_PHONE_SIX_TO_EIGHT_HOURS_A_DAY = '6 - 8 hours a day'
SMART_PHONE_EIGHT_PLUS_HOURS_A_DAY = '8 + hours a day'


SMART_PHONE_USAGE = (
(SMART_PHONE_LESS_THAN_ONE_HOUR_A_DAY, 'Less than one hour a day'),
(SMART_PHONE_ONE_TO_TWO_HOURS_A_DAY, '1 - 2 Hours a day'),
(SMART_PHONE_TWO_TO_FOUR_HOURS_A_DAY, '2 - 4 hours a day'),
(SMART_PHONE_FOUR_TO_SIX_HOURS_A_DAY, '4 - 6 hours a Day'),
(SMART_PHONE_SIX_TO_EIGHT_HOURS_A_DAY, '6 - 8 hours a day'),
(SMART_PHONE_EIGHT_PLUS_HOURS_A_DAY, '8 + hours a day'),
)

smart_phone_usage = models.CharField(null=True, blank=True, max_length=1, widget=models.Select(), choices=SMART_PHONE_USAGE, verbose_name='If Yes, How many hours a day do you access the Internet on it?')

我也曾尝试在 forms.py 中覆盖它,这是 SelectDateWidget 所必需的,但没有成功

我们一如既往地感谢您的帮助

谢谢

最佳答案

Django 模型不提供 RadioSelect 或 Select 小部件。您需要以模型形式添加它。

表单.py

class SmartPhoneForm(forms.ModelForm):
class Meta:
model = Phone
fields = ['smart_phone_ownership', 'smart_phone_usage']
widgets = {
'smart_phone_ownership': forms.RadioSelect,
'smart_phone_usage': forms.Select,
}

模型.py

class SmartPhone(models.Model):
# Do you own a Smartphone?
YES_SMARTPHONE = 'Yes'
NO_SMARTPHONE = 'No'


SMART_PHONE_OWNERSHIP = (
(YES_SMARTPHONE, 'Yes'),
(NO_SMARTPHONE, 'No'),
)
smart_phone_ownership = models.CharField(
null=True, max_length=1,
default=None,
choices=SMART_PHONE_OWNERSHIP, verbose_name='Do you own a Smartphone?')



#If 'Yes' How many hours a day do you access the Internet on it?
SMART_PHONE_LESS_THAN_ONE_HOUR_A_DAY = 'Less than one hour day'
SMART_PHONE_ONE_TO_TWO_HOURS_A_DAY = '1 - 2 Hours a day'
SMART_PHONE_TWO_TO_FOUR_HOURS_A_DAY = '2 - 4 hours a day'
SMART_PHONE_FOUR_TO_SIX_HOURS_A_DAY = '4 - 6 hours a day'
SMART_PHONE_SIX_TO_EIGHT_HOURS_A_DAY = '6 - 8 hours a day'
SMART_PHONE_EIGHT_PLUS_HOURS_A_DAY = '8 + hours a day'


SMART_PHONE_USAGE = (
(SMART_PHONE_LESS_THAN_ONE_HOUR_A_DAY, 'Less than one hour a day'),
(SMART_PHONE_ONE_TO_TWO_HOURS_A_DAY, '1 - 2 Hours a day'),
(SMART_PHONE_TWO_TO_FOUR_HOURS_A_DAY, '2 - 4 hours a day'),
(SMART_PHONE_FOUR_TO_SIX_HOURS_A_DAY, '4 - 6 hours a Day'),
(SMART_PHONE_SIX_TO_EIGHT_HOURS_A_DAY, '6 - 8 hours a day'),
(SMART_PHONE_EIGHT_PLUS_HOURS_A_DAY, '8 + hours a day'),
)

smart_phone_usage = models.CharField(
null=True, blank=True, max_length=1,
choices=SMART_PHONE_USAGE,
# default=None,
verbose_name='If Yes, How many hours a day do you access the Internet on it?'
)

关于django - 如何在 ModelForm 中指定 Select 和 RadioSelect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199471/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com