【发布时间】:2010-11-05 05:32:26
【问题描述】:
这就是我的做法,将表单中的布尔模型字段显示为单选按钮是和否。
choices = ( (1,'Yes'),
(0,'No'),
)
class EmailEditForm(forms.ModelForm):
#Display radio buttons instead of checkboxes
to_send_form = forms.ChoiceField(choices=choices,widget=forms.RadioSelect)
class Meta:
model = EmailParticipant
fields = ('to_send_email','to_send_form')
def clean(self):
"""
A workaround as the cleaned_data seems to contain u'1' and u'0'. There may be a better way.
"""
self.cleaned_data['to_send_form'] = int(self.cleaned_data['to_send_form'])
return self.cleaned_data
正如您在上面的代码中看到的,我需要一个干净的方法将输入字符串转换为整数,这可能是不必要的。
有没有更好和/或 djangoic 的方式来做到这一点。如果有,怎么做?
不,使用BooleanField 似乎会导致更多问题。使用它对我来说似乎很明显;但事实并非如此。为什么会这样。
【问题讨论】:
标签: django django-forms validation