1

这是我的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 address_to_photographer 为 False

你的 form.cleaned_data addr_to_photographer 是 False

你刚刚更新了我 oy278 地址到照片是 (u'False',)

但是当我查看 MySQL 中的字段时,它里面有一个 1,而不是 0。帮助!

4

2 回答 2

1

如果将选项移至模型字段定义会发生什么?
您可以跳过表单中的重复字段声明:

模型.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'

表格.py:

class EnterAppForm(ModelForm):
    class Meta:
        model = GraduationApp
于 2013-07-02T14:36:20.253 回答
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')
于 2013-07-02T19:27:25.747 回答