当我使用广播组部分填写表单(ModelForm)时,我收到此错误:invalid literal for int() with base 10: ''
我可以在某种程度上将''更改为0吗?以下是我的代码的相关部分
models.py
aggbs_0 = models.PositiveSmallIntegerField(verbose_name='Has Tantrums.', blank=True, default=int(0))
forms.py
nsf_CHOICES = (
('1', 'Never',),
('2', '',),
('3', 'Sometimes',),
('4', '',),
('5', 'Frequently',),
)
aggbs_0 = forms.ChoiceField(required=False, widget=forms.RadioSelect(renderer=HorizRadioRenderer), choices=nsf_CHOICES, label=Survey._meta.get_field_by_name('aggbs_0')[0].verbose_name)
views.py
def screen_students(request):
form = SurveyForm(request.POST or None)
if form.is_valid():
saved = form.save(commit=False)
saved.save()
messages.success(request, "submitted.")
return render_to_response("reports/screening.html",
locals(),
context_instance=RequestContext(request))
感谢您的帮助!
编辑:我将我的视图代码更改为此,现在一切正常,但我感觉有一种更好的方法可以将保存的更改''中的内容迭代到0。# get a set of students
@login_required
def screen_students(request):
form = SurveyForm(request.POST or None)
if form.is_valid():
saved = form.save(commit=False)
# If any radio group was left blank default to 0.
if saved.aggbs_0 == '':
saved.aggbs_0 = 0
if saved.aggbs_1 == '':
saved.aggbs_1 = 0
if saved.aggbs_2 == '':
saved.aggbs_2 = 0
if saved.aggbs_3 == '':
saved.aggbs_3 = 0
if saved.aggbs_4 == '':
saved.aggbs_4 = 0
if saved.aggbs_5 == '':
saved.aggbs_5 = 0
if saved.aggbs_6 == '':
saved.aggbs_6 = 0
if saved.aggbs_7 == '':
saved.aggbs_7 = 0
if saved.si_0 == '':
saved.si_0 = 0
if saved.si_1 == '':
saved.si_1 = 0
if saved.si_2 == '':
saved.si_2 = 0
if saved.si_3 == '':
saved.si_3 = 0
if saved.si_4 == '':
saved.si_4 = 0
if saved.si_5 == '':
saved.si_5 = 0
if saved.si_6 == '':
saved.si_6 = 0
if saved.si_7 == '':
saved.si_7 = 0
if saved.cfiab_0 == '':
saved.cfiab_0 = 0
if saved.cfiab_1 == '':
saved.cfiab_1 = 0
if saved.cfiab_2 == '':
saved.cfiab_2 = 0
if saved.cfiab_3 == '':
saved.cfiab_3 = 0
if saved.cfiab_4 == '':
saved.cfiab_4 = 0
if saved.cfiab_5 == '':
saved.cfiab_5 = 0
if saved.cfiab_6 == '':
saved.cfiab_6 = 0
if saved.cfiab_7 == '':
saved.cfiab_7 = 0
if saved.cfimb_0 == '':
saved.cfimb_0 = 0
if saved.cfimb_1 == '':
saved.cfimb_1 = 0
if saved.cfimb_2 == '':
saved.cfimb_2 = 0
if saved.cfimb_3 == '':
saved.cfimb_3 = 0
if saved.cfimb_4 == '':
saved.cfimb_4 = 0
if saved.cfimb_5 == '':
saved.cfimb_5 = 0
if saved.cfimb_6 == '':
saved.cfimb_6 = 0
if saved.cfimb_7 == '':
saved.cfimb_7 = 0
if saved.cfimb_8 == '':
saved.cfimb_8 = 0
saved.save()
messages.success(request, "submitted.")
return render_to_response("reports/survey_form.html",
locals(),
context_instance=RequestContext(request))