2013-07-02 29 views
0

下面是在我的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!

+0

你不應該在模型上設置'選擇= BOOLEAN_CHOICES'? – Hodson

+0

最後一個'print'語句中的'(u'False',)'是一個帶有unicode的元組。奇! –

+0

試試這個:'updapp.addr_to_photographer =布爾(form.cleaned_data [ 'addr_to_photographer'])' – karthikr

回答

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 
+0

我試圖把布爾周圍 updapp.addr_to_photographer =布爾(form.cleaned_data [ 'addr_to_photographer']) ,我回來了:你的POST address_to_photographer是假的 你的form.cleaned_data addr_to_photographer是假的 你只是UPDATEDDDD我helen ADDR TO PHOTO是(真的) 我唯一要說的是,至少事情是告訴我它是什麼真的保存了(True) - 但是,由於我試圖保存False,我不知道這是否更好? – HelenM

+0

什麼讓我感到困擾 - 什麼讓我覺得我的設置存在一些基本問題 - 是我會認爲django表單會爲我解決所有這些問題。首先,在將BOOLEAN_CHOICES添加到表單之前,它將渲染爲具有類似「未知」,「是」和「否」的下拉菜單作爲我的選項。在幕後,值是1,2和3 - 在我看來,這絕不會存儲0,1或者''作爲我的值,所以我嘗試了0,1和''AND True,False ,在幕後沒有,但他們沒有工作,要麼:P。 – HelenM

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') 
相關問題