2017-08-25 91 views
0

這與前段時間發佈的問題類似,但我遇到了不同的情況。我有一個附在模型上的模型。我得到一個非null約束,但沒有字段是null,所以約束失敗不應該出現。我不確定這個約束失敗的來源是什麼......有人可以幫助我,以防萬一我看不到它。NOT NULL約束失敗 - django

models.py

dob_month = models.IntegerField(default=0) 
dob_day = models.IntegerField(default=0) 
dob_year = models.IntegerField(default=0) 

views.py部分

dob_month = form.cleaned_data.get("dob_month") 
dob_day = form.cleaned_data.get("dob_day") 
dob_year = form.cleaned_data.get("dob_year") 

new_profile = Profile.objects.create(
       user = currentUser, 
       first_name = first_name, 
       last_name = last_name, 
       dob_month = dob_month, 
       dob_day = dob_day, 
       dob_year = dob_year, 
       city = city, 
       state = state, 
       phone = phone, 
       privacy = privacy, 
) 

形式POST請求

dob_month : '6' 
dob_day : '14' 
dob_year : '2019' 

這裏是即將到來的確切的錯誤...

IntegrityError at /setup_profile/ 
NOT NULL constraint failed: tab_profile.dob_day 
Request Method: POST 
Request URL: http://127.0.0.1:8000/setup_profile/ 
Django Version: 1.8.6 
Exception Type: IntegrityError 
Exception Value:  
NOT NULL constraint failed: tab_profile.dob_day 

修訂

這裏是forms.py部分...

class ProfileForm(forms.ModelForm): 
    split_choices = (('1', 'public'), 
        ('2', 'private')) 
    privacy = forms.TypedChoiceField(
     choices=split_choices, widget=forms.RadioSelect, coerce=int 
    ) 
    dob = forms.DateField(widget=extras.SelectDateWidget) 
    class Meta: 
     model = Profile 
     fields = ['first_name', 'last_name', 'dob', 'city', 'state', 'phone', 'privacy'] 

回答

1

我想這個問題是由於發生在數字領域

所以嘗試這個字符串插入,希望它會解決錯誤

new_profile = Profile.objects.create(
       user = currentUser, 
       first_name = first_name, 
       last_name = last_name, 
       dob_month = int(dob_month), 
       dob_day = int(dob_day), 
       dob_year = int(dob_year), 
       city = city, 
       state = state, 
       phone = phone, 
       privacy = privacy, 
) 
+0

我試着你說的,我仍然得到相同的錯誤...不知道如何解決它... @Exprator –

+0

我加文件的形式int eh更新部分的帖子......可以這樣...... @Exprator –

相關問題