这是我的models.py中的内容:
class GraduationApp(models.Model):
id = models.AutoField(primary_key=True)
addr_to_photographer = models.NullBooleanField(blank=True, null=True)
class Meta:
db_table = u'graduation_app'
这是我的forms.py中的内容:
BOOLEAN_CHOICES = (
(None,' '),
(True,'Yes'),
(False,'No')
)
class EnterAppForm(ModelForm):
addr_to_photographer = forms.ChoiceField(choices=BOOLEAN_CHOICES, required=False)
class Meta:
model = GraduationApp
fields=('addr_to_photographer')
以下是我模板中的内容:
Address to Photographer? {{ form.addr_to_photographer }}
这是我在views.py中的保存:
print 'your POST address_to_photographer is', request.POST['addr_to_photographer']
print 'your form.cleaned_data addr_to_photographer is', form.cleaned_data['addr_to_photographer']
updapp = GraduationAppForm.objects.get(id=request.POST['app_id'])
updapp.addr_to_photographer = form.cleaned_data['addr_to_photographer']
updapp.save()
print 'YOU JUST UPDATED ME', updapp.person.eid, 'ADDR TO PHOTO IS', updapp.addr_to_photographer
我收到这些印刷声明:
你的POST地址_to_photographer是假的
你的form.cleaned_data addr_to_photographer是假的
你刚刚更新了我oy278 ADDR TO PHOTO(u'False',)
但是当我去看看MySQL中的字段时,它有一个1,而不是0。 帮助!
答案 0 :(得分:1)
如果将选项移到模型字段定义会发生什么? 您可以跳过表格中的重复字段声明:
models.py:
BOOLEAN_CHOICES = (
(None,' '),
(True,'Yes'),
(False,'No')
)
class GraduationApp(models.Model):
id = models.AutoField(primary_key=True)
addr_to_photographer = models.NullBooleanField(choices=BOOLEAN_CHOICES, blank=True, null=True)
class Meta:
db_table = u'graduation_app'
forms.py:
class EnterAppForm(ModelForm):
class Meta:
model = GraduationApp
答案 1 :(得分:0)
解决方案是这样的:更新时,我需要获取我需要更新的记录,然后我需要使用我的帖子数据填充表单并参考我的记录实例。
updapp = GraduationApp.objects.get(id=request.POST['app_id'])
form = EnterAppForm(request.POST,instance=updapp)
我的forms.py实际上不需要任何东西,除了这个:
class EnterAppForm(ModelForm):
class Meta:
model = GraduationApp
fields=('status','name_in_pgm','addr_to_photographer','paper_form_reason_1','paper_form_reason_2','notes',\
'transfer_work_on_transcript','cbe_work_on_transcript','enrolled_other_institution',\
'enrolled_correspondence','getting_degree_other_ut_college','understand_min_gpa_req',\
'understand_no_drop_req','understand_pass_fail_req','understand_deg_requirements',\
'address_verified','eligibility_confirmed','placement_survey_complete')