下面是在我的models.py:Django爲什麼我的空布爾字段在我的SQL表中保存爲True?
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
我得到這些打印語句:
您的文章address_to_photographer是假
您form.cleaned_data addr_to_photographer是假
剛剛更新ME oy278 ADDR TO照片(U」 False'))
但是當我去看看MySQL中的字段時,它有一個1,而不是0. HEL P!
你不應該在模型上設置'選擇= BOOLEAN_CHOICES'? – Hodson
最後一個'print'語句中的'(u'False',)'是一個帶有unicode的元組。奇! –
試試這個:'updapp.addr_to_photographer =布爾(form.cleaned_data [ 'addr_to_photographer'])' – karthikr