我正在構建一個電子健康記錄軟件。我的一個觀點包含一個捕獲患者基本信息的表單。修復「date_of_birth」列中的空值違反了非空約束
forms.py
from django import forms
from nesting.models import Identity_unique
class Identity_Form(forms.ModelForm):
NIS = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter NIS',
'class' : 'form-control'
}
)
)
First_Name = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter First Name',
'class' : 'form-control'
}
)
)
Last_Name = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter Last Name',
'class' : 'form-control'
}
)
)
Residence = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter Address',
'class' : 'form-control'
}
)
)
DOB = forms.CharField(
widget=forms.TextInput(
attrs={
'placeholder': 'Enter Date of Birth',
'class' : 'form-control'
}
)
)
class Meta:
model = Identity_unique
fields = ('NIS', 'First_Name', 'Last_Name', 'Residence', 'DOB',)
爲的ModelForm
models.py
from django.db import models
from django.contrib.auth.models import User
from Identity import settings
import datetime
# Create your models here.
class Identity_unique(models.Model):
NIS = models.CharField(max_length = 200, primary_key = True)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
Timestamp = models.DateTimeField(auto_now = True)
First_Name = models.CharField(max_length = 80, null = True, )
Last_Name = models.CharField(max_length = 80, null = True,)
Residence = models.CharField(max_length = 80, blank = True)
DOB = models.DateField(auto_now = False)
不幸的是我不斷收到這個電子數據模式每次我嘗試提交保存的數據到服務器時出現錯誤:
IntegrityError at /nesting/
null value in column "date_of_birth" violates not-null constraint
DETAIL: Failing row contains (Q234934, 2, 2017-10-06 19:17:42.084063+00, Andre, James, [email protected], null, 1991-12-10).
我對數據庫使用postgresql 9.6。我刪除了遷移文件夾並多次遷移模型,但仍然存在null
和IntegrityError
。我也一直在我的數據庫中查找數據庫表Identity_unique,但我找不到它。我這樣做是爲了刪除空限制的列,因爲我將生日字段從date_of_birth = models.DateField(max_length = 100, default = 0)
更改爲DOB = models.DateField(auto_now = False).
正如您在錯誤中看到的那樣,列中有一個null
,這是不允許的。
最初當我使用date_of_birth = models.DateField(max_length = 100, null = True)
爲領域。系統提示我添加一個默認值,然後在bash終端中添加timezone.now()
。我不確定這解釋了錯誤背後的原因,但我仍然加入了它。在列